Multidimensional arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skallagrimur
Forum Newbie
Posts: 9
Joined: Fri Nov 12, 2010 4:33 am

Multidimensional arrays

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Multidimensional arrays

Post 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];
skallagrimur
Forum Newbie
Posts: 9
Joined: Fri Nov 12, 2010 4:33 am

Re: Multidimensional arrays

Post by skallagrimur »

Thanks a lot!
That was simple :))
Post Reply