php 数据集转换树、递归重组节点信息多维数组(转)
一、将数据集转换成树
/** * 将返回的数据集转换成树 * @param array $list 数据集 * @param string $pk 主键 * @param string $pid 父节点名称 * @param string $child 子节点名称 * @param integer $root 根节点ID * @return array 转换后的树 */ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root=0) { $tree = array();// 创建Tree 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[$data[$pk]] =& $list[$key]; }else{ if (isset($refer[$parentId])) { $parent =& $refer[$parentId]; $parent[$child][] =& $list[$key]; } } } } return $tree; }
转换前:
转换后:
array(3) { [11] => array(4) { ["code"] => int(11) ["name"] => string(9) "安徽省" ["sup_code"] => string(1) "0" ["son"] => array(2) { [0] => array(4) { ["code"] => int(114) ["name"] => string(9) "蚌埠市" ["sup_code"] => string(2) "11" ["son"] => array(1) { [0] => array(3) { ["code"] => int(1141) ["name"] => string(12) "龙子湖区" ["sup_code"] => string(3) "114" } } } [1] => array(4) { ["code"] => int(115) ["name"] => string(9) "芜湖市" ["sup_code"] => string(2) "11" ["son"] => array(1) { [0] => array(3) { ["code"] => int(1151) ["name"] => string(9) "弋江区" ["sup_code"] => string(3) "115" } } } } } [22] => array(4) { ["code"] => int(22) ["name"] => string(9) "浙江省" ["sup_code"] => string(1) "0" ["son"] => array(2) { [0] => array(4) { ["code"] => int(221) ["name"] => string(9) "杭州市" ["sup_code"] => string(2) "22" ["son"] => array(5) { [0] => array(3) { ["code"] => int(2211) ["name"] => string(9) "西湖区" ["sup_code"] => string(3) "221" } [1] => array(3) { ["code"] => int(2212) ["name"] => string(9) "上城区" ["sup_code"] => string(3) "221" } [2] => array(3) { ["code"] => int(2213) ["name"] => string(9) "下城区" ["sup_code"] => string(3) "221" } [3] => array(3) { ["code"] => int(2215) ["name"] => string(9) "拱墅区" ["sup_code"] => string(3) "221" } [4] => array(3) { ["code"] => int(2216) ["name"] => string(9) "萧山区" ["sup_code"] => string(3) "221" } } } [1] => array(4) { ["code"] => int(222) ["name"] => string(9) "嘉兴市" ["sup_code"] => string(2) "22" ["son"] => array(1) { [0] => array(3) { ["code"] => int(2221) ["name"] => string(9) "南湖区" ["sup_code"] => string(3) "222" } } } } } [33] => array(3) { ["code"] => int(33) ["name"] => string(9) "山东省" ["sup_code"] => string(1) "0" } }
二、递归重组节点信息多维数组字段
方法一
/** * 递归重组节点信息多维数组 * @param [array] $node [要处理的节点数组:二维数组] * @param [int] $root [根节点id] * @return [array] [树状结构的节点体系:多维数组] */ function node_merge($node,$root=0){ $arr = array(); foreach ($node as $v) { if ($v['pid'] == $root) { $v['child']=node_merge($node,$v['id']); $arr[]=$v; } } return $arr; }
方法二:
/** * 递归重组节点信息多维数组 * @param [array] $node [要处理的节点数组:二维数组] * @param [int] $pid [父级ID] * @param [array] $pidArr [父节点数组] * @return [array] [树状结构的节点体系:多维数组] */ function node_merge_gai($node,$pid=0,$pidArr){ $arr = array(); foreach ($node as $v) { if ($v['pid'] == $pid) { if(in_array($v['id'],$pidArr)){ $v['child']=node_merge($node,$v['id'],$pidArr); } $arr[]=$v; } } return $arr; }
调用方法如下图:
此处调用函数array_column是PHP5.5+版本才可支持,此处可以自定义方法,如下
/** * 二维数组转一维数组【实现低版本PHP支持】 * @param array $array 多维数组 * @param string $column_key 需要返回值的列 * @param string $index_key 用作返回数组的索引/键的列 * @return array() */ if (!function_exists("array_column")) { function array_column(array &$rows, $column_key, $index_key = null) { $data = array(); if (empty($index_key)) { foreach ($rows as $row) { $data[] = $row[$column_key]; } } else { foreach ($rows as $row) { $data[$row[$index_key]] = $row[$column_key]; } } return $data; } }
返回结果如:
array(4) { [0] => array(5) { ["id"] => int(1) ["name"] => string(5) "Index" ["title"] => string(6) "首页" ["pid"] => string(1) "0" ["child"] => array(2) { [0] => array(5) { ["id"] => int(9) ["name"] => string(8) "Category" ["title"] => string(6) "分类" ["pid"] => string(1) "1" ["child"] => array(0) { } } [1] => array(5) { ["id"] => int(10) ["name"] => string(5) "Store" ["title"] => string(6) "店铺" ["pid"] => string(1) "1" ["child"] => array(0) { } } } } [1] => array(5) { ["id"] => int(2) ["name"] => string(5) "Admin" ["title"] => string(6) "后台" ["pid"] => string(1) "0" ["child"] => array(4) { [0] => array(5) { ["id"] => int(5) ["name"] => string(5) "Trade" ["title"] => string(12) "交易管理" ["pid"] => string(1) "2" ["child"] => array(0) { } } [1] => array(5) { ["id"] => int(6) ["name"] => string(6) "Redbag" ["title"] => string(12) "红包管理" ["pid"] => string(1) "2" ["child"] => array(0) { } } [2] => array(5) { ["id"] => int(7) ["name"] => string(5) "Order" ["title"] => string(12) "订单管理" ["pid"] => string(1) "2" ["child"] => array(0) { } } [3] => array(5) { ["id"] => int(8) ["name"] => string(7) "Manager" ["title"] => string(9) "管理员" ["pid"] => string(1) "2" ["child"] => array(0) { } } } } [2] => array(5) { ["id"] => int(3) ["name"] => string(4) "Home" ["title"] => string(12) "图片中心" ["pid"] => string(1) "0" ["child"] => array(0) { } } [3] => array(5) { ["id"] => int(4) ["name"] => string(6) "Member" ["title"] => string(12) "会员中心" ["pid"] => string(1) "0" ["child"] => array(3) { [0] => array(5) { ["id"] => int(11) ["name"] => string(4) "Fund" ["title"] => string(6) "余额" ["pid"] => string(1) "4" ["child"] => array(0) { } } [1] => array(5) { ["id"] => int(12) ["name"] => string(7) "Product" ["title"] => string(6) "产品" ["pid"] => string(1) "4" ["child"] => array(3) { [0] => array(5) { ["id"] => int(14) ["name"] => string(3) "Out" ["title"] => string(9) "卖出的" ["pid"] => string(2) "12" ["child"] => array(0) { } } [1] => array(5) { ["id"] => int(15) ["name"] => string(4) "Sale" ["title"] => string(9) "在售的" ["pid"] => string(2) "12" ["child"] => array(0) { } } [2] => array(5) { ["id"] => int(16) ["name"] => string(2) "In" ["title"] => string(9) "仓库的" ["pid"] => string(2) "12" ["child"] => array(0) { } } } } [2] => array(5) { ["id"] => int(13) ["name"] => string(5) "Agent" ["title"] => string(12) "代理关系" ["pid"] => string(1) "4" ["child"] => array(0) { } } } } }