I kn开发者_高级运维ow 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.
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 = [[]];
?>
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.
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
?>
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;
}
If I want to create an empty array for handling lines from text files, I just use $array = array();
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
?>
// 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/>";
}
Remember that whenever you use array(array())
nested function or the short array syntax [[]]
both will create a 2D array with an empty
element in the first position. This may bring you some errors so we needs to be removed it.
How we can remove an empty
element?
Well it is absolute simple as calling the array_pop()
method to pop the element[0]
off the array, as this:
<?php
$a = array(array());
echo 'Before removing the empty element: \n'
print_r($a);
array_pop($a);
print_r($a);
$b = [[]];
echo 'Before removing the empty element: \n'
print_r($b);
array_pop($b);
print_r($b);
?>
精彩评论