|
...
|
...
|
@@ -144,3 +144,68 @@ if (!function_exists('checkDomain')) { |
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 把返回的数据集转换成Tree
|
|
|
|
* @param $list array 数据列表
|
|
|
|
* @param string|int $pk 主键|root
|
|
|
|
* @param string $pid 父id
|
|
|
|
* @param string $child 子键
|
|
|
|
* @param int $root 获取哪个id下面
|
|
|
|
* @param bool $empty_child 当子数据不存在,是否要返回空子数据
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$empty_child=true) {
|
|
|
|
// 如果是数字,则是root
|
|
|
|
if(is_numeric($pk)){
|
|
|
|
$root = $pk;
|
|
|
|
$pk = 'id';
|
|
|
|
}
|
|
|
|
// 创建Tree
|
|
|
|
$tree = array();
|
|
|
|
if(is_array($list)) {
|
|
|
|
// 创建基于主键的数组引用
|
|
|
|
$refer = array();
|
|
|
|
foreach ($list as $key => $data) {
|
|
|
|
if($empty_child){
|
|
|
|
$list[$key][$child] = [];
|
|
|
|
}
|
|
|
|
$refer[$data[$pk]] =& $list[$key];
|
|
|
|
}
|
|
|
|
foreach ($list as $key => $data) {
|
|
|
|
// 判断是否存在parent
|
|
|
|
$parentId = $data[$pid];
|
|
|
|
if ($root == $parentId) {
|
|
|
|
$tree[] =& $list[$key];
|
|
|
|
}else{
|
|
|
|
if (isset($refer[$parentId])) {
|
|
|
|
$refer[$parentId][$child][] = & $list[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tree数据转list
|
|
|
|
* @param $tree
|
|
|
|
* @param string $child
|
|
|
|
* @return array
|
|
|
|
* @author:dc
|
|
|
|
* @time 2022/1/11 10:13
|
|
|
|
*/
|
|
|
|
function tree_to_list($tree, $child='_child'){
|
|
|
|
$lists = [];
|
|
|
|
foreach ($tree as $item){
|
|
|
|
$c = $item[$child]??[];
|
|
|
|
unset($item[$child]);
|
|
|
|
$lists[] = $item;
|
|
|
|
if ($c){
|
|
|
|
$lists = array_merge($lists,tree_to_list($c, $child));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $lists;
|
|
|
|
} |
...
|
...
|
|