function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $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
$parentId = $data[$pid]
if ($root == $parentId) {
$tree[] =&$list[$key]
}else{
if (isset($refer[$parentId])) {
$parent =&$refer[$parentId]
$parent[$child][] =&$list[$key]
}
}
}
}
return $tree
}
然后定义一维数组为$list,然后 print_r(list_to_tree($list,"id","parentsid","subnav"))
微信小程序数组转字符串
var arr = [1,2,3,4,5,6]var str = arr.join(',')
console.log(str) // 1,2,3,4,5,6
您好。希望可以帮助到你!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)