php7将二维数组转为树状数组
在php7以上版本因为数组结构的改变很多网上转换为树状数组的实例已经无法使用了
public function list_to_tree($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]; } } } }