1

我需要我的 for 循环来返回一个看起来像这样的数组

    $nullArray =array(
 0 => array("id" => 1, "label" => "test 1", "type" => "folder"), 
      array("id" => 2, "label" => "test 2", "type" => "folder"),
      array("id" => 3, "label" => "test 3", "type" => "folder"),
      etc...
      etc... 
      etc...
);

我现在拥有的

 $nullArray = array();
$numOfVer = mysql_num_rows($result);
$startArray= array();

//SETS FIRST NODE
for($i =0;$i < $numOfVer;$i++)
{
    $label = mysql_result($result, $i);
    $id = $i+1;
    $startArray = array(array('id' => $id,'label' => $label, "type" => "folder"));


    //$startArray[]['id'] = $id;
    //$startArray[]['label'] = $label;
    //$startArray[]['type'] = "folder";
     //array_push($startArray,array(array('id' => $id,'label' => $label, "type" => "folder")));
    //$nullArray[0]= array(array('id' => $id,'label' => $label, "type" => "folder"));
    //array_push($nullArray[0],array('id' => $id,'label' => $label, "type" => "folder"));
}
$nullArray[0] = $startArray;

    echo json_encode($nullArray[0]);

我注释掉的所有内容都是我尝试过的,但都失败了。我已经为这么简单的事情做了太久了,所以我决定寻求帮助!先感谢您!:)

4

1 回答 1

1

在 for 循环中,您正在重新声明您的 $startArray 这就是删除先前值的原因。试试这个。

$nullArray = array();
$numOfVer = mysql_num_rows($result);
$startArray= array();

for($i =0;$i < $numOfVer;$i++)
{
    $label = mysql_result($result, $i);
    $id = $i+1;
    $startArray[] = array('id' => $id,'label' => $label, "type" => "folder");
}
于 2013-08-17T08:08:21.560 回答