View Full Version : multi-dimensional arrays

02-19-2006, 12:17 AM
I am studying php and am on a chapter dealing with arrays.
I am trying to write a script which prints to the screen 2 items in a multi-dimensional array. Now I read there are 2 ways to do a MD array
1. $master_array =array('array1','array2');


When I use the first method and put the code


its prints array1 item1

but when I use the same print code for the 2nd method it does nothing unless
I change the first item to the actual name of the array. i.e.


am I doing something wrong or is that how it works?

Kid Charming
02-19-2006, 01:42 AM
For each element of an array, there are two parts -- an index and a value. The value, of course, is the data you're putting into the array. The index is an identifier for that piece of data. Indexes can be numbers or strings. When you create an array or add an element to an existing array, you're setting both of these elements. But in method two:

$master_array =array('array1','array2');

you're only setting values. This is ok, though, because the array() construct recognizes this and creates numeric indexes (0 and 1, as you already know) for you.

In method two, though,


you're specifying that the string 'array1' is the index. In this case, array() will not create a numeric index for you, which is why trying to refer to it numerically won't work.

02-19-2006, 01:59 AM

Thanks for clearing that up. The tutorial I am following does not differentiate between the 2.