Page 1 of 1

Multidimensional arrays

Posted: Fri Nov 12, 2010 4:45 am
by skallagrimur
Hello, I have a, hopefully, small problem. I'm taking my first steps in PHP and I'm trying to work with multidimensional arrays. I have some background in Java.

What I'm trying to do, is read a text file and place the text that matches something predefined. When I would go about creating the multidimensional arrays for this in Java it would go like this:

Code: Select all

int[][][] MultiArray = new int[3][6][3]
All I can find when I search the web are these kind of predefined arrays:

Code: Select all

$a = array( 'color' => 'red',
            'taste' => 'sweet',
            'shape' => 'round',
            'name'  => 'apple',
            4        // key will be 0
          );
Hopefully I'm making myself clear. Any help appreciated.
Thanks

Re: Multidimensional arrays

Posted: Fri Nov 12, 2010 4:53 am
by Weirdan
Arrays in php are autoextending - you don't need to predefine their dimensions. Also you don't need to declare variable types as PHP is a dynamically typed language. So your example becomes just:

Code: Select all

$multiArray = array();
// set something
$multiArray[1][5][2] = 42;
// read back
echo $multiArray[1][5][2];

Re: Multidimensional arrays

Posted: Fri Nov 12, 2010 4:59 am
by skallagrimur
Thanks a lot!
That was simple :))