Jump to content

Multidimensional array?


eduard

Recommended Posts

I don´t understand example 2 of a multidimensional array (PHP tutorial): echo "Is" .$families[´Grifin´] [2]." a part of the Griffith family?";

Link to comment
Share on other sites

You'll need to understand the numeric and associative arrays in order to understand the multidimensional array. The basic idea is that an array element can be another array, so you could have an array of arrays. That's what this code does:

$families = array  (  "Griffin"=>array  (	"Peter",	"Lois",	"Megan"  ),  "Quagmire"=>array  (	"Glenn"  ),  "Brown"=>array  (	"Cleveland",	"Loretta",	"Junior"  ));

That code defines an associative array. The keys in the array are "Griffen", "Quagmire", and "Brown". The element $families['Griffen'] is another array, but it's numeric instead of associative, so the keys are 0, 1, and 2. So, to access the string "Megan", you use $families['Griffen'][2]. The "Griffen" tells it which array to find in $families, and the number tells it which element to find in the nested array. You could also do something like this:

$families = array  (  "Griffin"=>array  (	'father' => "Peter",	'mother' => "Lois",	'daughter' => "Megan"  ),  "Quagmire"=>array  (	"Glenn"  ),  "Brown"=>array  (	"Cleveland",	"Loretta",	"Junior"  ));

In that case, to access the string "Megan" you would use $families['Griffen']['daughter'].

Link to comment
Share on other sites

You'll need to understand the numeric and associative arrays in order to understand the multidimensional array. The basic idea is that an array element can be another array, so you could have an array of arrays. That's what this code does:
$families = array  (  "Griffin"=>array  (	"Peter",	"Lois",	"Megan"  ),  "Quagmire"=>array  (	"Glenn"  ),  "Brown"=>array  (	"Cleveland",	"Loretta",	"Junior"  ));

That code defines an associative array. The keys in the array are "Griffen", "Quagmire", and "Brown". The element $families['Griffen'] is another array, but it's numeric instead of associative, so the keys are 0, 1, and 2. So, to access the string "Megan", you use $families['Griffen'][2]. The "Griffen" tells it which array to find in $families, and the number tells it which element to find in the nested array. You could also do something like this:

$families = array  (  "Griffin"=>array  (	'father' => "Peter",	'mother' => "Lois",	'daughter' => "Megan"  ),  "Quagmire"=>array  (	"Glenn"  ),  "Brown"=>array  (	"Cleveland",	"Loretta",	"Junior"  ));

In that case, to access the string "Megan" you would use $families['Griffen']['daughter'].

Thanks very much! I understood a numeric and associative array, but by your explanation I also understand now the multidimensional array!
Link to comment
Share on other sites

You'll need to understand the numeric and associative arrays in order to understand the multidimensional array. The basic idea is that an array element can be another array, so you could have an array of arrays. That's what this code does:
$families = array  (  "Griffin"=>array  (	"Peter",	"Lois",	"Megan"  ),  "Quagmire"=>array  (	"Glenn"  ),  "Brown"=>array  (	"Cleveland",	"Loretta",	"Junior"  ));

That code defines an associative array. The keys in the array are "Griffen", "Quagmire", and "Brown". The element $families['Griffen'] is another array, but it's numeric instead of associative, so the keys are 0, 1, and 2. So, to access the string "Megan", you use $families['Griffen'][2]. The "Griffen" tells it which array to find in $families, and the number tells it which element to find in the nested array. You could also do something like this:

$families = array  (  "Griffin"=>array  (	'father' => "Peter",	'mother' => "Lois",	'daughter' => "Megan"  ),  "Quagmire"=>array  (	"Glenn"  ),  "Brown"=>array  (	"Cleveland",	"Loretta",	"Junior"  ));

In that case, to access the string "Megan" you would use $families['Griffen']['daughter'].

Thanks! Now I understand the [2]! Is daughter an associative arry or multidimensional or is daughter a sub-array of $famiilies?
Link to comment
Share on other sites

The whole thing

array ( 'father' => "Peter", 'mother' => "Lois", 'daughter' => "Megan" )

is an associative array.'daughter' is the key for one array member. In this case, the string "Megan".

Link to comment
Share on other sites

"daughter" is one of the keys in an associative array. It's the name of one of the elements in the $families['Griffin'] array, so it's the element $families['Griffin']['daughter'].
It´s not a sub-array or would it be like: $children of $Griffin?
Link to comment
Share on other sites

I'll try to explain it to you in the old "input-output" routine...When you have

$someArray['someKey']

What PHP is really doing is taking $someArray and 'key' as inputs for the "array selector" operation. Then, as output, it gives you the value of $someArray that corresponds to the 'someKey' string.That value can be anything. It can be a string, a number, or an array. In case you haven't guessed, the "array selector" operation is only available for arrays.Therefore, what you're doing with

$families['Griffin']['daughter']

is a sequence of two such input-output exchanges.First you're giving the $families array and 'Griffin' as inputs, and taking

array ( 'father' => "Peter", 'mother' => "Lois", 'daughter' => "Megan" )

as output. Then you give that same array above and 'daughter' as inputs, and take "Megan" as output.Try to think of the $families array like:

$families = array ( "Griffin"=>..., "Quagmire"=>..., "Brown"=>...);

And it should probably make more sence.

Link to comment
Share on other sites

In that case, it's only an element in the $families['Griffin'] array. You could also add a children array:

$families = array(  "Griffin"=>array  (	'father' => "Peter",	'mother' => "Lois",	'children' => array(	  'Meg',	  'Chris',	  'Stewie'	)  ));

Now you could access the string "Meg" at $families['Griffin']['children'][0]. "Chris" would be at $families['Griffin']['children'][1]. Using count($families['Griffin']['children']) would tell you how many children there are.

Link to comment
Share on other sites

The whole thing
array ( 'father' => "Peter", 'mother' => "Lois", 'daughter' => "Megan" )

is an associative array.'daughter' is the key for one array member. In this case, the string "Megan".

But can you have a sub-array $daughters if the array is $families?
Link to comment
Share on other sites

sure. these are only examples. in the real world they often get more nested and have more key's/values etc. There's no reason that you can't make it in anyway that suits your applications specific needs.

Link to comment
Share on other sites

sure. these are only examples. in the real world they often get more nested and have more key's/values etc. There's no reason that you can't make it in anyway that suits your applications specific needs.
Yep, like the one I'm using in my application, which has 5 dimensions: $arrPieces[0]['Frame']['HS'][1]['Part']
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...