【发布时间】:2016-03-14 08:29:51
【问题描述】:
我在为 jQuery JsTree 格式化 JSON 数据时遇到问题。我想在 Root 文件夹下显示许多子项。但是,当我不知道如何遍历所有子项时。以下代码仅打印 children 数组中的 1 个项目。我想打印所有子项。
$children = [];
foreach ($items as $item) {
$children['id'] = $item['id'];
$children['text'] = $item['title'];
}
$data = [
'id' => 1,
'text' => 'Root',
'children' => $children
];
echo json_encode($data);
以上代码打印如下结果:
{
"id":1,
"text":"Root",
"children":{
"id":"0B_J9f9IAYIKQczFkUERDUHpBYmc",
"text":"Complete Thesis.doc"
}
}
【问题讨论】: