No more verbosity, just go directly to the source code.
<?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, & amp;$sike=[]) { $r = array(); foreach($list as $k=>$item) { if($item['fid']==$p_id & amp; & amp; !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"=>"Home","id"=>'1',"fid"=>"0"], ["text"=>"Basic Information Management","id"=>'2',"fid"=>"0"], ["text"=>"Site information","id"=>'3',"fid"=>'2'], ["text"=>"Line information","id"=>'4',"fid"=>'2'], ]; $node_list1 = [ ["text"=>"Home","id"=>'2bd6db1b-4971-8a89-b2b7-e729ddb78ffa',"fid"=>"0"], ["text"=>"Basic Information Management","id"=>'376adc62-5414-a36d-eb49-65ae7c7a91b0',"fid"=>"0"], ["text"=>"Site information","id"=>'8ef4a669-68c0-48f7-5078-1d2a487ff901',"fid"=>'376adc62-5414-a36d-eb49-65ae7c7a91b0'], ["text"=>"Line information","id"=>'7b4a1c42-ec66-3fb5-3266-3686d2466bc9',"fid"=>'376adc62-5414-a36d-eb49-65ae7c7a91b0'], ]; $node_list2 = [ ["text"=>"Home","id"=>'2bd6db1b-4971-8a89-b2b7-e729ddb78ffa',"fid"=>"0"], ["text"=>"Basic Information Management","id"=>'b655b684-e112-5908-a522-f4f96728edb0',"fid"=>"0"], ["text"=>"Site information","id"=>'8ef4a669-68c0-48f7-5078-1d2a487ff901',"fid"=>'b655b684-e112-5908-a522-f4f96728edb0'], ["text"=>"Line information","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 "---------------------------------------->>Result 1<<- ----------------------------------".PHP_EOL; var_dump($menus_list); echo "---------------------------------------->>Result 2<<- ----------------------------------".PHP_EOL; var_dump($menus_list2); echo "Normal uuid format level".PHP_EOL; $menus_list3 = findChildren($node_list1, 0); $sike1 = []; $menus_list4 = findChildren2($node_list1, 0,$sike1); echo "---------------------------------------->>Result 3<<- ----------------------------------".PHP_EOL; var_dump($menus_list3); echo "---------------------------------------->>Result 4<<- ----------------------------------".PHP_EOL; var_dump($menus_list4); echo "Abnormal uuid format level".PHP_EOL; $menus_list5 = findChildren($node_list2, 0); $sike2 = []; $menus_list6 = findChildren2($node_list2, 0,$sike2); echo "---------------------------------------->>Result 5<<- ----------------------------------".PHP_EOL; var_dump($menus_list5); echo "---------------------------------------->>Result 6<<- ----------------------------------".PHP_EOL; var_dump($menus_list6);
Result output:
---------------------------------------->>Result 1<< ---------------------------------- array(2) { [0]=> array(3) { ["text"]=> string(6) "Homepage" ["id"]=> string(1) "1" ["fid"]=> string(1) "0" } [1]=> array(4) { ["text"]=> string(18) "Basic information management" ["id"]=> string(1) "2" ["fid"]=> string(1) "0" ["children"]=> array(2) { [0]=> array(3) { ["text"]=> string(12) "site information" ["id"]=> string(1) "3" ["fid"]=> string(1) "2" } [1]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(1) "4" ["fid"]=> string(1) "2" } } } } -------------------------------------------------->>Result 2<<--- ---------------------------------- array(2) { [0]=> array(3) { ["text"]=> string(6) "Homepage" ["id"]=> string(1) "1" ["fid"]=> string(1) "0" } [1]=> array(4) { ["text"]=> string(18) "Basic information management" ["id"]=> string(1) "2" ["fid"]=> string(1) "0" ["children"]=> array(2) { [0]=> array(3) { ["text"]=> string(12) "Site information" ["id"]=> string(1) "3" ["fid"]=> string(1) "2" } [1]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(1) "4" ["fid"]=> string(1) "2" } } } } Normal uuid format hierarchy -------------------------------------------------->>Result 3<<--- ---------------------------------- array(2) { [0]=> array(3) { ["text"]=> string(6) "Homepage" ["id"]=> string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa" ["fid"]=> string(1) "0" } [1]=> array(4) { ["text"]=> string(18) "Basic information management" ["id"]=> string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0" ["fid"]=> string(1) "0" ["children"]=> array(2) { [0]=> array(3) { ["text"]=> string(12) "Site information" ["id"]=> string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901" ["fid"]=> string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0" } [1]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9" ["fid"]=> string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0" } } } } -------------------------------------------------->>Result 4<<--- ---------------------------------- array(2) { [0]=> array(3) { ["text"]=> string(6) "Homepage" ["id"]=> string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa" ["fid"]=> string(1) "0" } [1]=> array(4) { ["text"]=> string(18) "Basic information management" ["id"]=> string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0" ["fid"]=> string(1) "0" ["children"]=> array(2) { [0]=> array(3) { ["text"]=> string(12) "Site information" ["id"]=> string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901" ["fid"]=> string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0" } [1]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9" ["fid"]=> string(36) "376adc62-5414-a36d-eb49-65ae7c7a91b0" } } } } Abnormal uuid format hierarchy -------------------------------------------------->>Result 5<<--- ---------------------------------- array(4) { [0]=> array(3) { ["text"]=> string(6) "Homepage" ["id"]=> string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa" ["fid"]=> string(1) "0" } [1]=> array(4) { ["text"]=> string(18) "Basic information management" ["id"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" ["fid"]=> string(1) "0" ["children"]=> array(2) { [0]=> array(3) { ["text"]=> string(12) "Site information" ["id"]=> string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901" ["fid"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" } [1]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9" ["fid"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" } } } [2]=> array(3) { ["text"]=> string(12) "Site information" ["id"]=> string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901" ["fid"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" } [3]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9" ["fid"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" } } -------------------------------------------------->>Result 6<<--- ---------------------------------- array(2) { [0]=> array(3) { ["text"]=> string(6) "Homepage" ["id"]=> string(36) "2bd6db1b-4971-8a89-b2b7-e729ddb78ffa" ["fid"]=> string(1) "0" } [1]=> array(4) { ["text"]=> string(18) "Basic information management" ["id"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" ["fid"]=> string(1) "0" ["children"]=> array(2) { [0]=> array(3) { ["text"]=> string(12) "site information" ["id"]=> string(36) "8ef4a669-68c0-48f7-5078-1d2a487ff901" ["fid"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" } [1]=> array(3) { ["text"]=> string(12) "Line information" ["id"]=> string(36) "7b4a1c42-ec66-3fb5-3266-3686d2466bc9" ["fid"]=> string(36) "b655b684-e112-5908-a522-f4f96728edb0" } } } }
Result 5 has a strange effect, that is, the child has also reached the same level as the parent. This is obviously not the result we want
findChildren2 was modified by me to avoid the situation of result 5.
The following is the description given by chatGPT
Final result modified using 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, & amp;$sike=[]) { $r = array(); foreach($list as $k=>$item) { //if($item['fid']==$p_id & amp; & amp; !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; }
Use this logical judgment: if (strcmp($item[‘fid’], $p_id) === 0) {}
Perfect solution! ! ! ! !