11

如何将值插入到 php 中的多维数组中?我需要使用 while 循环向数组中添加越来越多的行。

这是我的代码:

$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++;
}
4

3 回答 3

21
$size = 10;
$p = 0;
$myarray = array();
while($p < $size) {
  $myarray[] = array("number" => $data[$p], "data" => $kkk[1], "status" => "A");
  $p++;
}
于 2013-09-30T16:25:33.290 回答
3
$my_array = array()
foreach (range($p, $size-1) as $key) {
    array_push($my_array, array(
        "number" => $data[$key], 
        "data" => $kkk[1], 
        "status" => "A",
    ));
}
于 2013-09-30T16:29:10.837 回答
-1
$arr = new Array();
while($p<$size){
$arr[$p]["number"] = $data[$p];
$arr[$p]["data"] = $kkk[1];
$arr[$p]["status"] = "A";

$p++;
}
于 2013-09-30T16:26:57.887 回答