Creating an empty 2D array in PHP?

68,085

Solution 1

At its absolute simplest, a 2D dimensional array can be created as:

<?php
    $emptyArray = array(array());
?>

Or as of PHP 5.4 you can also use:

<?php
    $emptyArray = [[]];
?>

Solution 2

You don't create a 2d array in PHP, not in the traditional sense.

The suggestions above about $a = array(array()); in fact simply creating the following array:

$a = array(
    0 => array( )
);

Therefore, $a[0][0] = 'test'; would result in the following:

$a = array(
    0 => array(
        0 => 'test'
    )
);

At a first glance, it looks like it works, but in fact, this is still not a 2d array. When you try to use the 2nd row (index 1), at this point, PHP would throw a notice. Eg:

$a[1][0] = 'test2';

This is because $a[1] does not contain anything (remember that array(array()) simply creating an array at index 0?).

For it to work, you need to yet again do $a[1] = array();, or if you want to avoid indices you can do, $a[] = array();.


Example

$a = array(); // array of columns
for($c=0; $c<5; $c++){
    $a[$c] = array(); // array of cells for column $c
    for($r=0; $r<3; $r++){
        $a[$c][$r] = rand();
    }
}

The above code creates a 5x3 "2d array" of random numbers.

Solution 3

If I want to create an empty array for handling lines from text files, I just use $array = array();

Solution 4

Could you specify what you are trying to do? You can loop through multidimensional arrays with the foreach function

$ary=array( "subarr" => array("foo","bar") );

foreach($ary as $a){
  foreach($a as $ary_sub){
    echo $ary_sub;
  }
}

or

foreach($ary["subarr"] as $key=>$subval){
 echo $subval;
}

Solution 5

The PHP documentation is always a good way to start for these kind of basic questions.

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6];    // 5
echo $arr["somearray"][13];   // 9
echo $arr["somearray"]["a"];  // 42
?>
Share:
68,085
Joshua
Author by

Joshua

PHP/LaTeX

Updated on May 24, 2020

Comments

  • Joshua
    Joshua about 4 years

    I know that arrays are created dynamically, and creating them ahead of time isn't really necessary, but how would one do that with a 2D array? The same way?

    (for$j)
    {
    for($i)
        {
        $array[j][i] = "data";
        }
    }
    

    Something like that? Obviously real for loops, of course.

  • Joshua
    Joshua almost 13 years
    I looked at the multidimensional array documentation, there are no examples for creating an empty one.
  • Joshua
    Joshua almost 13 years
    Thank you. That's all I needed.
  • Brendan Bullen
    Brendan Bullen almost 13 years
    @Joshua No problem. I think people downvoted because they didn't understand the reasoning behind the question. Not a good reason to downvote. That's what comments are for. Keep on asking questions... :)
  • windsound
    windsound over 10 years
    @Joshua time will reveal the truth!
  • RST
    RST over 9 years
    I wondered where my empty entries came from when using 2-dim arrays. Changed $arr[]=array(); at top of function to $arr[$id]=array(); where it is used and all is fine again
  • radleybobins
    radleybobins over 7 years
    Thank you for sharing this. I thought I was going crazy trying to figure out how I created a blank element at [0]
  • Herbert Van-Vliet
    Herbert Van-Vliet about 3 years
    Careful though, as print_r( $emptyArray ) shows it isn't truly empty (that would be hard as removing the inner [] would render it a 1D array).