/    Sign up×
Community /Pin to ProfileBookmark

Multidimensional Arrays

Here is what I came up with:

[code=php]
$family = array

Array
(
[Smith] => Array
(
[0] => Gary
[1] => Alex
[2] => Lisa
[3] => John
[4] => Nic
)
[Jones] => Array
(
[0] => Allan
[1] => Darren
[2] => Karen
[3] => Robert
[4] => Mark
)
)

echo “Is $family[Smith][2] from the Matrix?”;
[/code]

It says there is a mistake on this line:

[code=php][Smith] => Array[/code]

Here is the example from the website. It does work.

[code=php]
$families = array
(
“Griffin”=>array
(
“Peter”,
“Lois”,
“Megan”
),
“Quagmire”=>array
(
“Glenn”
),
“Brown”=>array
(
“Cleveland”,
“Loretta”,
“Junior”
)
);

echo “Is ” . $families[‘Griffin’][2] .
” a part of the Griffin family?”;
[/code]

Then it comes up with this and I cant seem to get that to work.

[code=php]
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 04.2008 — [code=php]<?php

$family = Array(
'Griffin' => Array(
0 => 'Peter',
1 => 'Lois',
2 => 'Megan'
),
'Quagmire' => Array(
0 => 'Glenn'
),
'Brown' => Array(
0 => 'Cleveland',
1 => 'Loretta',
2 => 'Junior'
)
);


echo "meap {$family['Griffin'][0]} him.";
?>[/code]

EDIT: added description and improved spacing, technicaly nothing's changed.
Copy linkTweet thisAlerts:
@Hooded_VillianauthorApr 04.2008 — Why the hell would they post their example up differently?
Copy linkTweet thisAlerts:
@scragarApr 04.2008 — it's no different in result:

The version I posted uses the number references because I didn't want you to be confused and continue to use the square bracketsand cause more errors.

Secondly I used:
{$family['Griffin'][0]}to enclose the var, this prevents PHP interpriting the variable's value as anything different(say printing "Array[0]" or some such)
Copy linkTweet thisAlerts:
@Hooded_VillianauthorApr 04.2008 — Your way makes much more sense and works perfectly.

I prefer using the numbered refrences... Makes things a little easier at the moment.

That mix up and confusion doesn't instill much of my confidence in www.w3schools.com to be honest, as that's where I am studying from.

Shot for yout help once again dude...
Copy linkTweet thisAlerts:
@NogDogApr 04.2008 — ...

Then it comes up with this and I cant seem to get that to work.

[code=php]
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)
[/code]
[/QUOTE]

FYI, that is the resulting [i]output[/i] of a print_r() function; it is not actual PHP source code for creating the array. (I have not viewed that tutorial, so I do not know if it is an actual mistake, or just not clearly enough written for you to realize what they were doing at the time.)
Copy linkTweet thisAlerts:
@Hooded_VillianauthorApr 04.2008 — Dude... U are so right.

[code=html]The array above would look like this if written to the output:[/code]

Didn't quiet know what it meant. Thought it was another way of typing the code. Whoops!!!
×

Success!

Help @Hooded_Villian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...