2 回答
TA贡献1773条经验 获得超3个赞
如果您尝试推送第二个循环,请在第一个循环中声明您的变量。
$arr1 = array();
foreach(){
$arr2 = array();
foreach(){
}
}
TA贡献1943条经验 获得超7个赞
这现在有效
通过在第一个 foreach 循环中声明变量 $docs 为空数组...感谢 marjameson
//Declare global empty array
$nodes = [];
foreach($getStations as $key => $step){
//Declare docs empty array
$docs = [];
foreach($docs as $key=>$val) {
$docs[] = array(
"id" =>"$val[0]",
"title" => "$val[2]",
"class" => "color$color"
);
}//second foreach
$nodes[] = array(
"id"=>"step_ID",
"title"=>"Some Title",
"class"=>"Some Class",
"item" =>$docs //Pushing the array $docs
);
}//end main foreach
//Display the result
echo json_encode($nodes);
添加回答
举报