php根据id和pid把单级数组重新组合为树结构

function list_to_trees($list, $pk='id', $pid = 'pid', $child = 'children', $root = 0) {
//创建Tree
$tree = array();
if (is_array($list)) {
//创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data){
$refer[$data[$pk]] = &$list[$key];
}
foreach ($list as $key => $data){
//判断是否存在parent
$parantId = $data[$pid];
if ($root == $parantId) {
$tree[] = &$list[$key];
}else {
if (isset($refer[$parantId])) {
$parent = &$refer[$parantId];
$parent[$child][] = &$list[$key];
}
}
}
}
return $tree;
}
参数说明
- $list [array] 原始数组
- $pk [string] id
- $pid [string] 父级pid
- $child [array] 承载子集的容器
- return [array] 返回树结构
一般传第一个参数就行
联系站长:
相关文章
js获取m到n随机数,js获取随机整数,从0到10,从m到n任意数
˂a class="reference-link" name="js获取从m到n的随机数"˃js获取从m到n的随机数function getRandomNumber(min, ma...
fingerprintjs2使用方法,以及fingerprintjs2文件下载
fingerprintjs2使用方法,以及fingerprintjs2文件下载获取唯一标识,如果有技术问题可以联系站长官方教程:<script> // ...
js实用函数之map()函数的使用,一分钟学会
重点:map() 方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。值得注意的是:1、map()函数不会对空数组进行检测;2、map...
vue history路由下,对ngnix服务配置修改防止出现404问题
打开配置文件: # 打开配置文件 vi /usr/local/nginx/conf/nginx.con 宝塔则点击域名在里面找到配置文件进去修改: 如果域名直接指向...
js获取本月,本年,近一月,近3月,近1年的日期范围,获取近n月的日期范围
js获取本月,本年,近一月,近3月,近1年的日期范围,获取近n月的日期范围function getnMonth(i) { console.log(&quo...
如何使用宝塔配置正向代理,例如:请求https://www.自己的域名.com/api,代理到https://www.别人的域名.com/api
ngnix如何使用宝塔配置正向代理,例如:请求https://www.自己的域名.com/api, 代理到https://www.别人的域名.com/apilocation /baidu/&...




