我有这样的输入,也许数据越来越多:
[ [ "id" => "uuid-1", "createdAt" => "2021-02-26T00:55:36.632Z", "name" => "Webprovise Corp", "parentId" => "0" ], [ "id" => "uuid-2", "createdAt" => "2021-02-25T10:35:32.978Z", "name" => "Stamm LLC", "parentId" => "uuid-1" ], [ "id" => "uuid-3", "createdAt" => "2021-02-25T15:16:30.887Z", "name" => "Blanda, Langosh and Barton", "parentId" => "uuid-1" ], [ "id" => "uuid-4", "createdAt" => "2021-02-25T06:11:47.519Z", "name" => "Price and Sons", "parentId" => "uuid-2" ], [ "id" => "uuid-5", "createdAt" => "2021-02-25T13:35:57.923Z", "name" => "Hane - Windler", "parentId" => "uuid-3" ], [ "id" => "uuid-6", "createdAt" => "2021-02-26T01:41:06.479Z", "name" => "Vandervort - Bechtelar", "parentId" => "uuid-3" ], [ "id" => "uuid-7", "createdAt" => "2021-02-25T07:56:32.335Z", "name" => "Zieme - Mills", "parentId" => "uuid-2" ] ]
我想要的输出是,只使用 php 来制作它:
[ "uuid-1" => [ "id" => "uuid-1", "createdAt" => "2021-02-26T00:55:36.632Z", "name" => "Webprovise Corp", "childrens" => [ [ "id" => "uuid-2", "createdAt" => "2021-02-25T10:35:32.978Z", "name" => "Stamm LLC", "parentId" => "uuid-1", "childrens" => [ [ "id" => "uuid-4", "createdAt" => "2021-02-25T06:11:47.519Z", "name" => "Price and Sons", "parentId" => "uuid-2" ], [ "id" => "uuid-7", "createdAt" => "2021-02-25T07:56:32.335Z", "name" => "Zieme - Mills", "parentId" => "uuid-2" ] ] ], [ "id" => "uuid-3", "createdAt" => "2021-02-25T15:16:30.887Z", "name" => "Blanda, Langosh and Barton", "parentId" => "uuid-1", "childrens" => [ [ "id" => "uuid-5", "createdAt" => "2021-02-25T13:35:57.923Z", "name" => "Hane - Windler", "parentId" => "uuid-3" ], [ "id" => "uuid-6", "createdAt" => "2021-02-26T01:41:06.479Z", "name" => "Vandervort - Bechtelar", "parentId" => "uuid-3" ] ] ], ] ] ]
我可以使用 javascript 在网络浏览器中轻松地显示这个菜单树,但我不知道只有 php。我该怎么做呢?也许它有很多很多深儿童的水平。请给我一个想法。
希望这会对你有所帮助
<?php $arr=[ [ "id" => "uuid-1", "createdAt" => "2021-02-26T00:55:36.632Z", "name" => "Webprovise Corp", "parentId" => "0" ], [ "id" => "uuid-2", "createdAt" => "2021-02-25T10:35:32.978Z", "name" => "Stamm LLC", "parentId" => "uuid-1" ], [ "id" => "uuid-3", "createdAt" => "2021-02-25T15:16:30.887Z", "name" => "Blanda, Langosh and Barton", "parentId" => "uuid-1" ], [ "id" => "uuid-4", "createdAt" => "2021-02-25T06:11:47.519Z", "name" => "Price and Sons", "parentId" => "uuid-2" ], [ "id" => "uuid-5", "createdAt" => "2021-02-25T13:35:57.923Z", "name" => "Hane - Windler", "parentId" => "uuid-3" ], [ "id" => "uuid-6", "createdAt" => "2021-02-26T01:41:06.479Z", "name" => "Vandervort - Bechtelar", "parentId" => "uuid-3" ], [ "id" => "uuid-7", "createdAt" => "2021-02-25T07:56:32.335Z", "name" => "Zieme - Mills", "parentId" => "uuid-2" ], [ "id" => "uuid-8", "createdAt" => "2021-02-26T00:55:36.632Z", "name" => "Webprovise Corp", "parentId" => "0" ], ]; function findChild($uuid){ global $arr; return array_filter($arr,function($value) use($uuid){ return $value['parentId']==$uuid; }); } function getChilds($childrens){ $newarr=[]; foreach ($childrens as $child) { $childs=findChild($child['id']); count($childs)?$child['childrens']=getChilds($childs):""; $newarr[$child['id']]=$child; } return $newarr; } function getParents(){ global $arr; return array_filter($arr,function($value){ return $value['parentId']=="0"; }); } function newArray(){ $newarr=[]; foreach (getParents() as $parent) { $childs=findChild($parent['id']); count($childs)?$parent['childrens']=getChilds($childs):""; $newarr[$parent['id']]=$parent; } return $newarr; } print_r(newArray());
输出:
{ "uuid-1":{ "id":"uuid-1", "createdAt":"2021-02-26T00:55:36.632Z", "name":"Webprovise Corp", "parentId":"0", "childrens":{ "uuid-2":{ "id":"uuid-2", "createdAt":"2021-02-25T10:35:32.978Z", "name":"Stamm LLC", "parentId":"uuid-1", "childrens":{ "uuid-4":{ "id":"uuid-4", "createdAt":"2021-02-25T06:11:47.519Z", "name":"Price and Sons", "parentId":"uuid-2" }, "uuid-7":{ "id":"uuid-7", "createdAt":"2021-02-25T07:56:32.335Z", "name":"Zieme - Mills", "parentId":"uuid-2" } } }, "uuid-3":{ "id":"uuid-3", "createdAt":"2021-02-25T15:16:30.887Z", "name":"Blanda, Langosh and Barton", "parentId":"uuid-1", "childrens":{ "uuid-5":{ "id":"uuid-5", "createdAt":"2021-02-25T13:35:57.923Z", "name":"Hane - Windler", "parentId":"uuid-3" }, "uuid-6":{ "id":"uuid-6", "createdAt":"2021-02-26T01:41:06.479Z", "name":"Vandervort - Bechtelar", "parentId":"uuid-3" } } } } }, "uuid-8":{ "id":"uuid-8", "createdAt":"2021-02-26T00:55:36.632Z", "name":"Webprovise Corp", "parentId":"0" } }