一维数组生成树形结构

Tree.php

<?php
class Tree{
    //要处理的数组
    private $arrSource = array();
    //配置
    private $config = array(
        'key'  => 'id', //主键
        'pKey' => 'pid', //主键父级名称
        'child'=> 'child'//子节点的名称
    );

    /**
     * @param mixed $arrSource
     */
    public function setArrSource($arrSource)
    {
        $this->arrSource = $arrSource;
    }

    /**
     * @param array $config
     */
    public function setConfig($config)
    {
        $this->config = $config;
    }

    /**
     * 对原始的数组进行排序,保证子节点排在父节点的前面
     */
    public function sortArr(){
        $tempArr = array();
        foreach ($this->arrSource as $k => $v){
            $tempArr[$k] = $v[$this->config['pKey']];
        }
        array_multisort($tempArr,SORT_DESC ,SORT_NUMERIC ,$this->arrSource);
    }

    /**
     * 生成树形结构
     */
    public function getTree(){
        foreach ($this->arrSource as $k => $v){
            if($this->checkParent($v[$this->config['pKey']])){
                //父级元素存在,把本元素放在父级元素里面,然后删除本元素
                $this->pushParent($k);
            }
        }
        return $this->arrSource;
    }

    /**
     * 检查父级是否存在,存在返回true,否则,返回false
     * @param $parentKey
     * @return bool
     */
    private function checkParent($parentKey){
        $parentArr = array_column($this->arrSource,$this->config['key']);
        if(in_array($parentKey,$parentArr)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 把子节点放到对应的父节点里面,并删除子节点
     * @param $key
     */
    private function pushParent($key){
        $current = $this->arrSource[$key];
        foreach ($this->arrSource as $k => $v){
            if($v[$this->config['key']] == $current[$this->config['key']]){
                $childKey = $k;
            }
            if($v[$this->config['key']] == $current[$this->config['pKey']]){
                $parentKey = $k;
            }
        }
        if(isset($childKey) && isset($parentKey)){
            $this->arrSource[$parentKey][$this->config['child']][] = $this->arrSource[$childKey];
            unset($this->arrSource[$childKey]);
        }
    }
}
?>

TreeTest.php

<?php
include 'Tree.php';
$obj = new Tree();
//设置节点名称,默认为id,pid,child,如果是默认的,不用设置
$config = array(
    'key'  => 'id', //主键
    'pKey' => 'pid', //主键父级名称
    'child'=> 'child'//子节点的名称
);
$obj->setConfig($config);
$arr = array(
	array('id' => 1,'name'=>'广东','pid'=>0),
	array('id' => 2,'name'=>'广州','pid'=>1),
	array('id' => 3,'name'=>'罗湖','pid'=>4),
	array('id' => 4,'name'=>'深圳','pid'=>1),
	array('id' => 5,'name'=>'保安','pid'=>4)
);
$obj->setArrSource($arr);
//通过排序,保证子节点出现在父节点的前面,如果子节点在父节点前面,不用调用此方法
$obj->sortArr();
$result = $obj->getTree();
echo '<pre>';
print_r($result);
?>

 结果如下:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值