Click to See Complete Forum and Search --> : Multidimensional Arrays


Hooded_Villian
04-03-2008, 06:26 PM
Here is what I came up with:


$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?";


It says there is a mistake on this line:

[Smith] => Array

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


$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?";


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


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

scragar
04-03-2008, 07:03 PM
<?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.";
?>
EDIT: added description and improved spacing, technicaly nothing's changed.

Hooded_Villian
04-03-2008, 07:08 PM
Why the hell would they post their example up differently?

scragar
04-03-2008, 07:13 PM
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)

Hooded_Villian
04-03-2008, 07:19 PM
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...

NogDog
04-03-2008, 07:55 PM
...
Then it comes up with this and I cant seem to get that to work.


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

FYI, that is the resulting output 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.)

Hooded_Villian
04-04-2008, 02:53 AM
Dude... U are so right.

The array above would look like this if written to the output:

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