<?php
// 数据
$data = [
[
'id' => 1,
'file' => 'order.php',
'name' => '订单管理',
'pid' => 0
],
[
'id' => 2,
'file' => 'order_stats.php',
'name' => '订单统计',
'pid' => 1
],
[
'id' => 3,
'file' => 'pack.php',
'name' => '包装管理程序',
'pid' => 1
],
[
'id' => 4,
'file' => 'delivery_man.php',
'name' => '订单发货',
'pid' => 0
],
[
'id' => 5,
'file' => 'delivery_list.php',
'name' => '发货列表',
'pid' => 4
]
];
function getChildren($item, $data)
{
$pid = $item == null ? 0 : $item['id'];
$res = [];
foreach ($data as $v) {
if ($v['pid'] == $pid) {
$v['children'] = getChildren($v, $data);
$res[] = $v;
}
}
return $res;
}
function formatData($data)
{
$newData = getChildren(null, $data);
echo "<pre>";
foreach ($newData as $v) {
echo "| |-" . $v['file'] . $v['name'] . '<br />';
foreach ($v['children'] as $i => $o) {
echo str_repeat(" ", $i + 1) . "| |-" . $o['file'] . $o['name'] . '<br />';
}
}
echo "</pre>";
}
formatData($data);