为 jsTree 生成的树
function createJsonTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent']) {
if ($currLevel > $prevLevel) $output .= ' , "children":[ ';
if ($currLevel == $prevLevel) $output .= " }, ";
$output .= '{ "data" :'.'"'.$category['menu_title'].'"';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
$output .= self::createJsonTree($array, $category['id'], $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) $output .= " }] ";
return $output;
}
function createJsonTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
foreach ($array as $categoryId => $category) {
if ($currentParent == $category['parent']) {
if ($currLevel > $prevLevel) $output .= ' , "children":[ ';
if ($currLevel == $prevLevel) $output .= " }, ";
$output .= '{ "data" :'.'"'.$category['menu_title'].'"';
if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
$currLevel++;
$output .= self::createJsonTree($array, $category['id'], $currLevel, $prevLevel);
$currLevel--;
}
}
if ($currLevel == $prevLevel) $output .= " }] ";
return $output;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
json_encode 不起作用有什么原因吗?
https://www.php.net/json_encode
Is there a reason that json_encode won't work for this?
https://www.php.net/json_encode
您可以像这样生成一棵树:
You can generate a tree like this: