28 lines
803 B
PHP
28 lines
803 B
PHP
<?php
|
|
// 这是系统自动生成的公共文件
|
|
|
|
if (!function_exists('array_to_tree')) {
|
|
/**
|
|
* 数组转换为树状结构
|
|
* @param array $data 数据
|
|
* @param int $pid 父级ID
|
|
* @param string $with 转换依据字段
|
|
* @param int $level 层级
|
|
* @return array
|
|
*/
|
|
function array_to_tree(array $data, int $pid, string $with = 'pid', int $level = 1)
|
|
{
|
|
$ret = [];
|
|
foreach ($data as $item) {
|
|
if ($item[$with] == $pid) {
|
|
$item['level'] = $level;
|
|
$children = array_to_tree($data, $item['id'], $with, $level + 1);
|
|
if ($children) {
|
|
$item['children'] = $children;
|
|
}
|
|
$ret[] = $item;
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
} |