PHP递归获取相关节点

		$temp = [
	        [
	            'id'=>12,
                'sid'=>1,
                'pid'=>999,
                'gid'=>999,
            ],
            [
                'id'=>13,
                'sid'=>2,
                'pid'=>999,
                'gid'=>999,
            ],
        ];
        $found_key = array_search(40489, array_column($userdb, 'uid'));
		
		
		$meng = new Changeofscene;
        $array = $meng->queryChange();
		echo "<pre>";
		print_r($array);exit();
		
		
		, [
            'id' => 64,
            'se' => 5,
        ], [
            'id' => 68,
            'se' => 5,
        ]
		
		
		
		
		$data = [[
            'id' => 12,
            'pid'=> 9,
            'se' => 1,
        ], [
            'id' => 13,
            'pid'=> 9,
            'se' => 1,
        ]];

        $meng = new Changeofscene;
        $array = $meng->queryChange();
		
		public function findOne($data,$all){
        $newArr = [];
        foreach ($data as $k=>$v){
            $newArr[] = $v['id'];
            $newArr[] = $v['pid'];
            $found_keys = array_search($v['pid'], array_column($array, 'value'));
            if($found_keys){
                $newArr[] = $array[$v['pid']]['sequence'];
            }
        }
        echo "<pre>";
        print_r($newArr);exit();
    }
	
	
	
	
	
	$data = [ [
            'id' => 167,//实时购买->赎回->理财产品->理财保险->个人业务场景->场景
            'pid'=> 153,
            'se' => 5,
        ], [
            'id' => 12,
            'pid'=> 9,
            'se' => 1,
        ], [
            'id' => 147,//签约
            'pid'=> 96,
            'se' => 5,
        ]];
	//获取上级所有节点
    private function parent_tree_data($list, $child, &$trees = [])
    {
        $tree = array();
        foreach ($list as $k => $row) {
            if($row['value'] == $child) {
                $trees[] = $row['sequence'];
                $this->parent_tree_data($list, $row['sequence'],$trees);
                //$row['parent'] = $this->parent_tree_data($list, $row['sequence'],$trees);
                //$tree[] = $row;
            }
        }
        return $trees;
    }
    //获取下级所有节点
    private function children_tree_data($list, $child, &$trees = [])
    {
        $tree = array();
        foreach ($list as $k => $row) {
            if ($row['sequence'] == $child) {
                $trees[] = $row['value'];
                $this->children_tree_data($list, $row['value'], $trees);
                //$row['children'] = $this->children_tree_data($list, $row['value'], $trees);
                //$tree[] = $row;
            }
        }

        return $trees;
    }
    //获取相关所有节点
    public function findOne($data, $array)
    {
        $newArr = [];
        foreach ($data as $k => $v) {
            $newArr[] = $v['id'];
            $newArr[] = $v['pid'];
            $newArr = array_merge($newArr,$this->parent_tree_data($array, $v['pid']),$this->children_tree_data($array, $v['id']));
            //$newArr[] = $this->parent_tree_data($array, $v['pid']);
            //$newArr[] = $this->children_tree_data($array, $v['id']);
        }
        $newArr = array_unique($newArr);
        sort($newArr);
        /*echo "<pre>";
        print_r($newArr);exit();*/
        return $newArr;
    }
		

  

posted @ 2019-10-31 13:47  MeetU  阅读(407)  评论(0编辑  收藏  举报