$data = [
[
'first' => 1101,
'second' => 1102,
'third' => 1103
],
[
'first' => 1111,
'second' => 1112,
'third' => 1113
],
];
$data是需要进行排序的数组
下面进行数组操作
$arr = [];
foreach($data as $v){
//一级
if(!key_exists($v['first'],$arr)){
$arr[$v['first']] = [
'value' => $v['first'],
'children' => []
];
}
//二级
if(!key_exists($v['second'],$arr[$v['first']]['children'])){
$arr[$v['first']]['children'][$v['second']] = [
'value' => $v['second'],
'children' => []
];
}
//三级
$arr[$v['first']]['children'][$v['second']]['children'][$v['third']] = [
'value' => $v['third']
];
}
排序结果
将排序结果进行处理
foreach($arr as $key => $value){
foreach ($value['children'] as $m => $item) {
$arr[$key]['children'][$m]['children'] = array_values($item['children']);
}
$arr[$key]['children'] = array_values($arr[$key]['children']);
}
$arr = array_values($arr);
最后排序结果