PHP数组练习

一、对数组[21,25,11,32,12,35,55,77,66]进行排序,输出结果为:[77 11 66 12 55 21 35 25 32]

//排序 结果:77 11  66 12 55 21 35 25 32
        $arr = [21,25,11,32,12,35,55,77,66];
        rsort($arr);
        $i = 0;
        $j = 8;
        for ($k=0;$k<=8;$k++){
            if($k%2==0){
                $arrs[] = $arr[$i];
                $i = $i+1;
            }else{
                $arrs[] = $arr[$j];
                $j = $j-1;
            }
        }
        echo "<pre>";
        print_r($arr);
        echo "<pre>";
        print_r($arrs);
        exit();

二、php 递归 返回数组

private function tree_data(&$list, $parent){
            $tree = array();
            foreach($list as $row) {
                if($row['permission_parent_id'] == $parent) {

                    $row['children'] = $this->tree_data($list, $row['permission_id']);
                    $tree[] = $row;
                }
            }
            return $tree;
}

三、查找子节点上下级

$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;
    }
    $meng = new Changeofscene;
    $array = $meng->queryChange();
posted @ 2019-07-04 17:57  MeetU  阅读(527)  评论(0编辑  收藏  举报