Drezard Posted March 5, 2008 Share Posted March 5, 2008 Whats the fastest way to set a 2D array? I've found multiple different ways but, I cant find one thats fast. What is the fastest way (or way you prefer?) Thanks, Daniel Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 5, 2008 Share Posted March 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
keeB Posted March 5, 2008 Share Posted March 5, 2008 He's asking for 2 dimensional (2D) arrays. Basically, you don't instantiate them All kidding aside, my preferred method is the following: <?php $d2 = array("dimension2" => "this is dimension2"); $d1 = array("dimension1" => $d1); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.