How do I declare a two-dimensional array?

What is the easiest way to create a 2d array. I was hoping I could do something like this:

declare int d[0..m, 0..n] 
+81
arrays php
Nov 28 '09 at 0:20
source share
12 answers

You can also create an associative array or a hash table array by specifying the index of the array.

 $array = array( 0 => array( 'name' => 'John Doe', 'email' => 'john@example.com' ), 1 => array( 'name' => 'Jane Doe', 'email' => 'jane@example.com' ), ); 

Which is equivalent

 $array = array(); $array[0] = array(); $array[0]['name'] = 'John Doe'; $array[0]['email'] = 'john@example.com'; $array[1] = array(); $array[1]['name'] = 'Jane Doe'; $array[1]['email'] = 'jane@example.com'; 
+77
Nov 28 '09 at 0:30
source share

The following equivalents lead to a two-dimensional array:

 $array = array( array(0, 1, 2), array(3, 4, 5), ); 

or

 $array = array(); $array[] = array(0, 1, 2); $array[] = array(3, 4, 5); 
+63
Nov 28 '09 at 0:23
source share

Just announce? You donโ€™t have to. Just make sure the variable exists:

 $d = array(); 

Arrays dynamically change, and trying to write something to a non-exsistant element creates it (and creates a solid array if necessary)

 $d[1][2] = 3; 

This is valid for any number of dimensions without prior declarations.

+35
Nov 28 '09 at 0:34
source share

Firstly, PHP does not have multidimensional arrays; it has arrays of arrays.

Secondly, you can write a function that will do this:

 function declare($m, $n, $value = 0) { return array_fill(0, $m, array_fill(0, $n, $value)); } 
+26
Nov 28 '09 at 0:30
source share

For a simple โ€œfill as you goโ€ solution:

$foo = array(array());

This will give you a flexible pseudo-two-dimensional array that can contain $ foo [n] [n], where n <= โˆž (of course, your limit is limited by memory sizes, but you get the idea that I hope). This could theoretically be expanded to create as many auxiliary arrays as possible.

+11
Nov 19 '11 at 7:01
source share

Or for large arrays, all with the same value:

 $m_by_n_array = array_fill(0, $n, array_fill(0, $m, $value); 

will create an array $m by $n with the value set to $value .

+8
Nov 28 '09 at 0:26
source share

As far as I know, there is no built-in php function for this, you need to do it through a loop or using a custom method that recursively calls something like array_fill, distorted in @Amber's answer;

I assume that you want to create an empty array with arrays of arrays. For example, you want to get the final results, as shown below, from an array of 3 arrays:

  $final_array = array(array(), array(), array()); 

This is simple for simple code, but for an array of arbitrary size, such as an array of 3 arrays of 3 arrays, it starts to get complicated initialization before use:

  $final_array = array(array(array(), array(), array()), array(array(), array(), array()), array(array(), array(), array())); 

... etc...

I get disappointed. It would be nice to have a simple way to declare an initialized array of arrays of any depth so that you can use it without checking or throwing errors.

+2
Oct 13 '14 at 20:34
source share

And for me, the argument about whether the array should be sparse or not depends on the context.

For example, if $ a [6] [9] is not filled, this is equivalent to $ a [6] [9], filled with, for example, the character "" or 0.

+2
Jul 12 '15 at 7:40
source share
 $r = array("arr1","arr2"); 

to repeat one element of the array you should write:

 echo $r[0]; echo $r[1]; 

: arr1 arr2

+2
Jul 11 '17 at 11:29 on
source share

And I like this way:

 $cars = array ( array("Volvo",22), array("BMW",15), array("Saab",5), array("Land Rover",17) ); 
0
Dec 28 '18 at 18:32
source share

If you want to quickly create a multidimensional array for a simple value using a single line, I would recommend using this array library to do it like this:

 $array = Arr::setNestedElement([], '1.2.3', 'value'); 

which will produce

 [ 1 => [ 2 => [ 3 => 'value' ] ] ] 
0
Feb 26 '19 at 19:37
source share

Utley's answer really helped me figure this out. Here is an example of how to iterate over a two-dimensional array. This example shows how to find values โ€‹โ€‹for known array names, as well as foreach, in which you simply look at all the fields that you find there. Hope this helps someone.

 $array = array( 0 => array( 'name' => 'John Doe', 'email' => 'john@example.com' ), 1 => array( 'name' => 'Jane Doe', 'email' => 'jane@example.com' ), ); foreach ( $array as $groupid => $fields) { echo "hi element ". $groupid . "\n"; echo ". name is ". $fields['name'] . "\n"; echo ". email is ". $fields['email'] . "\n"; $i = 0; foreach ($fields as $field) { echo ". field $i is ".$field . "\n"; $i++; } } 

Outputs:

 hi element 0 . name is John Doe . email is john@example.com . field 0 is John Doe . field 1 is john@example.com hi element 1 . name is Jane Doe . email is jane@example.com . field 0 is Jane Doe . field 1 is jane@example.com 
0
Mar 08 '19 at 22:03
source share



All Articles