Home » Php » Creating an empty 2D array in PHP?

Creating an empty 2D array in PHP?

Posted by: admin November 29, 2017 Leave a comment

Questions:

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.

Answers:

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 = [[]];
?>

Questions:
Answers:

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 5×3 “2d array” of random numbers.

Questions:
Answers:

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
?>

Questions:
Answers:

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

Questions:
Answers:

Using “array(array())” will create a 2D array with an “empty” element in the first position. To create a truly blank 2D array this needs to be removed.

<?php
    $emptyArray = array(array());  // Creates a 2D array with one empty element in $emptyArray[0]
    array_pop($emptyArray);        // Pops element[0] off the array
?>

Questions:
Answers:

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;
}

Questions:
Answers:
// dynamic 2D array

$twoD = array(array());
$val = 0;

// fill the array
for($r = 0; $r < 4; $r++) {
    for($c = 0; $c < 4; $c++) 
        $twoD[$r][$c] = $val++;
}

// print the current value of $val
echo $val."<br/>------------------<br/>";

// print the array  
for($r = 0; $r < 4; $r++) {
    for($c = 0; $c < 4; $c++) 
        echo $twoD[$r][$c];
    echo "<br/>";
}