您好,我有一个数组,其中包含两个其他数组,使用以下代码:
foreach($relations as $rel){
$data[$i]["relationTo"] = $rel["name"];
$data[$i]["relation"] = $rel["relation"];
$i = $i+1;
}
foreach($relations as $rel){
$children[$i]["id"] = $rel["id2"];
$children[$i]["name"] = $rel["sname"];
$children[$i]["data"] = $data;
$i = $i+1;
}
foreach($relations as $rel){
$relationArray[$i]["id"] = $rel["id"];
$relationArray[$i]["name"] = $rel["name"];
$relationArray[$i]["children"] = $children;
$i = $i+1;
}
当我使用以下方法打印出来时:
print_r($relationArray);
它打印以下内容:
Array ( [2] => Array ( [id] => 4 [name] => Albaraa [children] =>
Array ( [1] => Array ( [id] => 5 [name] => Sadi [data] =>
Array ( [0] => Array ( [relationTo] => Albaraa [relation] => Father ) ) ) ) ) )
我正在使用 json_encode 并且我需要它以某种方式在 json 中输出,不包括开头的数组的索引计数......我使用时的 json 输出:
echo json_encode($relationArray);
目前是这样的:
{"2":{"id":"4","name":"Albaraa","children":
{"1":{"id":"5","name":"Sadi","data": [{"relationTo":"Albaraa","relation":"Father"}]}}}}
在前 2 个数组的前面加上“2”和“1”……这不是我想要实现的,它是这样的:
{"id":"4","name":"Albaraa","children":
{"id":"5","name":"Sadi","data": [{"relationTo":"Albaraa","relation":"Father"}]}}}}
任何帮助都感激不尽!