Php operation array
for($i=1;$i<=5;$i++){array(array($i));}
The for loop contains a two-dimensional array. I want to merge the two-dimensional array in the for loop and print the result as follows:
array( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) [4] => Array ( [0] => 5 ))
Reply to discussion (solution)
$rs = array();for($i=0; $i<=5; $i++) { $rs[] = array($i);}
$rs = array();for($i=0; $i<=5; $i++) { $rs[] = array($i);}
This method does not work. The array ($ I) on the right must be a two-dimensional array.
$rs = array();for($i=0; $i<=5; $i++) { $rs[] = array($i);}
This method does not work. The array ($ I) on the right must be a two-dimensional array.
Have you run my code?
I know the result is correct, but you cannot do this.
$rs[] = array($i);
The right side must be:
array(array($i))
$ Rs = array (); for ($ I = 0; $ I <= 5; $ I ++) {$ tmp = array ($ I )); // do you want to do this? $ Rs [] = $ tmp [0];}
$ Rs = array (); for ($ I = 0; $ I <= 5; $ I ++) {$ tmp = array ($ I )); // do you want to do this? $ Rs [] = $ tmp [0];}
This method does not work. you can only get the number of $ I. If there are other elements in the two-dimensional array, you cannot get it.
for($i=1;$i<=5;$i++){ $res[$i] = array(array($i));}
for($i=1;$i<=5;$i++){ $res[$i] = array(array($i));}
The result is a three-dimensional array.
$res = array();for($i=1;$i<=5;$i++){ $res = array_merge($res, array(array($i)));}print_r($res);
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
[4] => Array
(
[0] => 5
)
)
OK, positive solution. thank you, boss.