How can I insert values into a multidimensional array in php? I need to add more and more rows to array using a while loop.
如何在php中将值插入多维数组?我需要使用while循环向数组添加越来越多的行。
Here is my code:
这是我的代码:
$size=10;
$p=0;
while($p<$size)
{
$myarray = array(
array("number"=>$data[$p],"data"=>$kkk[1],"status"=>"A"),
array("number"=>$data[$p],"data"=>$kkk[1],"status"=>"A"));
// Each time the loop executes, I need to add more array to myarray.
$p++;
}
3 个解决方案
#1
15
$size = 10;
$p = 0;
$myarray = array();
while($p < $size) {
$myarray[] = array("number" => $data[$p], "data" => $kkk[1], "status" => "A");
$p++;
}
#2
3
$my_array = array()
foreach (range($p, $size-1) as $key) {
array_push($my_array, array(
"number" => $data[$key],
"data" => $kkk[1],
"status" => "A",
));
}
#3
0
$arr = new Array();
while($p<$size){
$arr[$p]["number"] = $data[$p];
$arr[$p]["data"] = $kkk[1];
$arr[$p]["status"] = "A";
$p++;
}
#1
15
$size = 10;
$p = 0;
$myarray = array();
while($p < $size) {
$myarray[] = array("number" => $data[$p], "data" => $kkk[1], "status" => "A");
$p++;
}
#2
3
$my_array = array()
foreach (range($p, $size-1) as $key) {
array_push($my_array, array(
"number" => $data[$key],
"data" => $kkk[1],
"status" => "A",
));
}
#3
0
$arr = new Array();
while($p<$size){
$arr[$p]["number"] = $data[$p];
$arr[$p]["data"] = $kkk[1];
$arr[$p]["status"] = "A";
$p++;
}