不啰嗦 直接上源代码
<?php
function findChildren($list, $p_id){
$r = array();
foreach ($list as $k => $item) {
if ($item['fid'] == $p_id) {
unset($list[$k]);
$length = count($r);
$r[$length] = $item;
if ($t = findChildren($list, $item['id'])) {
$r[$length]['children'] = $t;
}
}
}
return $r;
}
function findChildren2($list,$p_id,&$sike=[]) {
$r = array();
foreach($list as $k=>$item) {
if($item['fid']==$p_id && !in_array($item['id'],$sike)) {
$menu = $item;
array_push($sike,$item['id']);
$children = findChildren2($list,$item['id'],$sike);
if(!empty($children)) {
$menu['children'] = $children;
}
$r[] = $menu;
}
}
return $r;
}
$node_list = [
["text"=>"首页","id"=>'1',"fid"=>"0"],
["text"=>"基础信息管理","id"=>'2',"fid"=>"0"],
["text"=>"站点信息","id"=>'3',"fid"=>'2'],
["text"=>"线路信息","id"=>'4',"fid"=>'2'],
];
$node_list1 = [
["text"=>"首页","id"=>'2bd6db1b-4971-8a89-b2b7-e729ddb78ffa',"fid"=>"0"],
["text"=>"基础信息管理","id"=>'376adc62-5414-a36d-eb49-65ae7c7a91b0',"fid"=>"0"],
["text"=>"站点信息","id"=>'8ef4a669-68c0-48f7-5078-1d2a487ff901',"fid"=>'376adc62-5414-a36d-eb49-65ae7c7a91b0'],
["text"=>"线路信息","id"=>'7b4a1c42-ec66-3fb5-3266-3686d2466bc9',"fid"=>'376adc62-5414-a36d-eb49-65ae7c7a91b0'],
];
$node_list2 = [
["text"=>"首页","id"=>'2bd6db1b-4971-8a89-b2b7-e729ddb78ffa',"fid"=>"0"],
["text"=>"基础信息管理","id"=>'b655b684-e112-5908-a522-f4f96728edb0',"fid"=>"0"],
["text"=>"站点信息","id"=>'8ef4a669-68c0-48f7-5078-1d2a487ff901',"fid"=>'b655b684-e112-5908-a522-f4f96728edb0'],
["text"=>"线路信息","id"=>'7b4a1c42-ec66-3fb5-3266-3686d2466bc9',"fid"=>'b655b684-e112-5908-a522-f4f96728edb0'],
];
$menus_list = findChildren($node_list, 0);
$sike = [];
$menus_list2 = findChildren2($node_list, 0,$sike);
echo "----------------------------------------->>结果1<<------------------------------------".PHP_EOL;
var_dump($menus_list);
echo "----------------------------------------->>结果2<<------------------------------------".PHP_EOL;
var_dump($menus_list2);
echo "正常的uuid格式的层级".PHP_EOL;
$menus_list3 = findChildren($node_list1, 0);
$sike1 = [];
$menus_list4 = findChildren2($node_list1, 0,$sike1);
echo "----------------------------------------->>结果3<<------------------------------------".PHP_EOL;
var_dump($menus_list3);
echo "----------------------------------------->>结果4<<------------------------------------".PHP_EOL;
var_dump($menus_list4);
echo "不正常的uuid格式的层级".PHP_EOL;
$menus_list5 = findChildren($node_list2, 0);
$sike2 = [];
$menus_list6 = findChildren2($node_list2, 0,$sike2);
echo "----------------------------------------->>结果5<<------------------------------------".PHP_EOL;
var_dump($menus_list5);
echo "----------------------------------------->>结果6<<------------------------------------".PHP_EOL;
var_dump($menus_list6);
结果输出:
----------------------------------------->>结果1<<------------------------------------
array(2) {
[0]=>
array(3) {
["text"]=>
string(6) "首页"
["id"]=>
string(1) "1"
["fid"]=>
string(1) "0"
}
[1]=>
array(4) {
["text"]=>
string(18) "基础信息管理"
["id"]=>
string(1) "2"
["fid"]=>
string(1) "0"
["children"]=>
array(2) {
[0]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(1) "3"
["fid"]=>
string(1) "2"
}
[1]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(1) "4"
["fid"]=>
string(1) "2"
}
}
}
}
----------------------------------------->>结果2<<------------------------------------
array(2) {
[0]=>
array(3) {
["text"]=>
string(6) "首页"
["id"]=>
string(1) "1"
["fid"]=>
string(1) "0"
}
[1]=>
array(4) {
["text"]=>
string(18) "基础信息管理"
["id"]=>
string(1) "2"
["fid"]=>
string(1) "0"
["children"]=>
array(2) {
[0]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(1) "3"
["fid"]=>
string(1) "2"
}
[1]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(1) "4"
["fid"]=>
string(1) "2"
}
}
}
}
正常的uuid格式的层级
----------------------------------------->>结果3<<------------------------------------
array(2) {
[0]=>
array(3) {
["text"]=>
string(6) "首页"
["id"]=>
string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa"
["fid"]=>
string(1) "0"
}
[1]=>
array(4) {
["text"]=>
string(18) "基础信息管理"
["id"]=>
string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0"
["fid"]=>
string(1) "0"
["children"]=>
array(2) {
[0]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901"
["fid"]=>
string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0"
}
[1]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9"
["fid"]=>
string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0"
}
}
}
}
----------------------------------------->>结果4<<------------------------------------
array(2) {
[0]=>
array(3) {
["text"]=>
string(6) "首页"
["id"]=>
string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa"
["fid"]=>
string(1) "0"
}
[1]=>
array(4) {
["text"]=>
string(18) "基础信息管理"
["id"]=>
string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0"
["fid"]=>
string(1) "0"
["children"]=>
array(2) {
[0]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901"
["fid"]=>
string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0"
}
[1]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9"
["fid"]=>
string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0"
}
}
}
}
不正常的uuid格式的层级
----------------------------------------->>结果5<<------------------------------------
array(4) {
[0]=>
array(3) {
["text"]=>
string(6) "首页"
["id"]=>
string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa"
["fid"]=>
string(1) "0"
}
[1]=>
array(4) {
["text"]=>
string(18) "基础信息管理"
["id"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
["fid"]=>
string(1) "0"
["children"]=>
array(2) {
[0]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901"
["fid"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
}
[1]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9"
["fid"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
}
}
}
[2]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901"
["fid"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
}
[3]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9"
["fid"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
}
}
----------------------------------------->>结果6<<------------------------------------
array(2) {
[0]=>
array(3) {
["text"]=>
string(6) "首页"
["id"]=>
string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa"
["fid"]=>
string(1) "0"
}
[1]=>
array(4) {
["text"]=>
string(18) "基础信息管理"
["id"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
["fid"]=>
string(1) "0"
["children"]=>
array(2) {
[0]=>
array(3) {
["text"]=>
string(12) "站点信息"
["id"]=>
string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901"
["fid"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
}
[1]=>
array(3) {
["text"]=>
string(12) "线路信息"
["id"]=>
string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9"
["fid"]=>
string(36) "b655b684-e112-5908-a522-f4f96728edb0"
}
}
}
}
结果5 出现了一个诡异的效果 也就是子级也跑到和父级一个层级了 这显然不是我们想要的结果
findChildren2 是我为了避免结果5 这种情况而进行改造了
下面是chatGPT 给出的说明
最终结果 修改 使用strcmp
function findChildren($list, $p_id){
$r = array();
foreach ($list as $k => $item) {
//if ($item['fid'] == $p_id) {
if (strcmp($item['fid'], $p_id) ===0 ) {
unset($list[$k]);
$length = count($r);
$r[$length] = $item;
if ($t = findChildren($list, $item['id'])) {
$r[$length]['children'] = $t;
}
}
}
return $r;
}
function findChildren2($list,$p_id,&$sike=[]) {
$r = array();
foreach($list as $k=>$item) {
//if($item['fid']==$p_id && !isset($sike[$item['id']])) {
if (strcmp($item['fid'], $p_id) ===0 ) {
$menu = $item;
//$sike[$item['id']] = true;
$children = findChildren2($list,$item['id'],$sike);
if(!empty($children)) {
$menu['children'] = $children;
}
$r[] = $menu;
}
}
return $r;
}
使用该逻辑判断 :if (strcmp($item['fid'], $p_id) === 0) {}
完美解决!!!!!