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把数字格式化为千分位兼容版,兼容小数(8,888.22)
js把任意数字格式化为千分位/** * 格式化为千分位 * @param num 当前值字符串 * @ret...
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/&...
Animate.css使用方法,及源码下载,包含中文文档教程-css3动画演示合集,可用于uniapp
Animate.css是一个即用型跨浏览器动画库,可在您的 Web 项目中使用。非常适合强调、主页、滑块和注意力引导提示。注意:Animate.css可以在uniapp中使用,并且兼容多端,源码放页面...
js获取m到n随机数,js获取随机整数,从0到10,从m到n任意数
˂a class="reference-link" name="js获取从m到n的随机数"˃js获取从m到n的随机数function getRandomNumber(min, ma...
原生js把时间戳转为日期格式年月日时分秒
原生js把时间戳转为日期格式年月日时分秒function parsetime(timestamp){ let timelength...






