一个递归方法
function organizeRecords($regions)
{
$organizedRegions = [];
foreach ($regions as $region) {
$organizedRegions[$region['id']] = $region;
$organizedRegions[$region['id']]['children'] = [];
}
return $organizedRegions;
}
function buildTree($organized)
{
$tree = [];
foreach ($organized as $id => $record) {
if ($record['pid']) {
$organized[$record['pid']]['children'][] = &$organized[$id];
} else {
$tree[] = &$organized[$id];
}
}
return $tree;
}
$organized=organizeRecords(需要递归的数据);
$data=buildTree($organized);
print_r($data);
标签:organizedRegions,递归函数,tree,region,organized,record,高性能,PHP,id
From: https://www.cnblogs.com/renloong/p/18309167