【原创】请尊重逻辑处理方案!
感谢~终于搞出来了!如有大神可以用循环优化至自动N级
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站
<?
$menu = file_get_contents("json/menu.json");
$menu_arr = json_decode($menu, true);
$fid = [];
foreach ($menu_arr as $value) {
if (empty($fid[$value['fid']])) {
$fid[$value['fid']] = [];
}
array_push($fid[$value['fid']], $value);
}
//echo json_encode($fid);
foreach ($fid as $value) {
}
$menu_1 = $fid['0'];
$finish = [];
foreach ($fid['0'] as $value1) { // 一级目录
$value1['child'] = [];
if (!empty($fid[$value1['id']])) {
foreach ($fid[$value1['id']] as $value2) { // 二级目录
$value2['child'] = [];
if (!empty($fid[$value2['id']])) {
foreach ($fid[$value2['id']] as $value3) { // 三级目录
$value3['child'] = [];
if (!empty($fid[$value3['id']])) {
foreach ($fid[$value3['id']] as $value4) { // 四级目录
$value4['child'] = [];
if (!empty($fid[$value4['id']])) {
foreach ($fid[$value4['id']] as $value5) { // 五级目录
$value5['child'] = [];
array_push($value4['child'], $value5);
}
}
array_push($value3['child'], $value4);
}
}
array_push($value2['child'], $value3);
}
}
array_push($value1['child'], $value2);
}
}
array_push($finish, $value1);
}
echo json_encode($finish);
// foreach ($fid[$value1['id']] as $value2) { // 三级目录
// array_push($value1['child'], $value2);
// }
【www.6661314.xyz】我是初云--欢迎访问!!!含分享交流站