Jump to content

[PHP5] Declaring 2D arrays


Drezard

Recommended Posts

You do not have to declare arrays in PHP... but assigning them, there are several ways

 

$arr[] = 'test1';

$arr[] = 'test2';

 

will populate $arr as such:

 

$arr[0] contains 'test1';

$arr[1] contains 'test2';

 

You can also use the array() function

 

$arr = array('test1', 'test2');

 

will populate $arr as such:

 

$arr[0] contains 'test1';

$arr[1] contains 'test2';

 

Alternately, you can define them with user keys with the array function

 

$arr = array('keyA' => 'test1', 'keyB' => 'test2');

 

will populate $arr as such:

 

$arr['keyA'] contains 'test1';

$arr['keyB'] contains 'test2';

 

You can also define them directly, as so

 

$arr['keyA'] = 'test1';

$arr[1] = 'test2';

 

To iterate through an array, the easiest way is to use the foreach() function, as described here

http://php.net/foreach

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.