Convert Java Array

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
alvinmeister
Forum Newbie
Posts: 1
Joined: Mon Oct 03, 2011 9:48 am

Convert Java Array

Post by alvinmeister »

int array[][] ={{1,2,3},{4,5,6},{7,8,9}};


int array1[][] ={{1,2,3},{4,5,6},{7,8,9}};


int array2[][] = new int[3][3];

new to php.

thanks in advance.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Convert Java Array

Post by Celauran »

Unlike Java, you don't explicitly define array sizes in PHP.

Code: Select all

int array[][] = new int[3][3]
is, therefore, unnecessary. PHP is also weakly typed, so specifying that the array will contain ints is equally unnecessary. Moreover, arrays in PHP are not restricted to a single type.

Your first two arrays look the same to me, and can be written thus:

Code: Select all

$arrayName = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));
You could also specify key names:

Code: Select all

$arrayName = array(
                'key1' => array(1, 2, 3),
                'key2' => array(4, 5, 6),
                'key3' => array(7, 8, 9)
             );
Post Reply