根据分类实现无限级

数组

$array = [
    [
        "username"=>"xiaxian2",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"99500.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "userid"=>"09b42ce90879719a67f050b431b03534",
        "zongliushui"=>"500",
        "zongyinkui"=>null
    ],
    [
        "username"=>"erjixiaxian2",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"99400.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "userid"=>"2a1ab4890db2142c6c0bb51ef77994fb",
        "zongliushui"=>"600",
        "zongyinkui"=>null
    ],
    [
        "username"=>"erjixiaxian",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"100592.80",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "userid"=>"6256d265dcc1e9cfca2c37c1093444cb",
        "zongliushui"=>"600",
        "zongyinkui"=>"592.8"
    ],
    [
        "username"=>"admin",
        "headimg"=>"/Templates/home/res/face/2.png",
        "money"=>"11038.83",
        "yongjin"=>"1.50",
        "yongjingai"=>"1",
        "jiesuan"=>"7.50",
        "agent"=>"7d9e993765d4f79748ec54cbed9a996e",
        "userid"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "zongliushui"=>"5821",
        "zongyinkui"=>"8351.548"
    ],
    [
        "username"=>"xiaxian1",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"97494.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "userid"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "zongliushui"=>"3500",
        "zongyinkui"=>"-2006"
    ]
];

引用算法

//引用算法
function generateTree($array){
    //第一步 构造数据
    $items = array();
    foreach($array as $value){
        $items[$value['userid']] = $value;
    }
    //第二部 遍历数据 生成树状结构
    $tree = array();
    foreach($items as $key => $value){
        if(isset($items[$value['agent']])){
            $items[$value['agent']]['son'][] = &$items[$key];
        }else{
            $tree[] = &$items[$key];
        }
    }
    return $tree;
}

$a = generateTree($array);
dd($a);

结果

[
    {
        "username": "admin",
        "headimg": "/Templates/home/res/face/2.png",
        "money": "11038.83",
        "yongjin": "1.50",
        "yongjingai": "1",
        "jiesuan": "7.50",
        "agent": "7d9e993765d4f79748ec54cbed9a996e",
        "userid": "8e5d35bf1414296e03c7846ebab7af6e",
        "zongliushui": "5821",
        "zongyinkui": "8351.548",
        "son": [
            {
                "username": "xiaxian2",
                "headimg": "/Templates/home/res/face/3.png",
                "money": "99500.00",
                "yongjin": "0.00",
                "yongjingai": "0",
                "jiesuan": "0.00",
                "agent": "8e5d35bf1414296e03c7846ebab7af6e",
                "userid": "09b42ce90879719a67f050b431b03534",
                "zongliushui": "500",
                "zongyinkui": null
            },
            {
                "username": "xiaxian1",
                "headimg": "/Templates/home/res/face/3.png",
                "money": "97494.00",
                "yongjin": "0.00",
                "yongjingai": "0",
                "jiesuan": "0.00",
                "agent": "8e5d35bf1414296e03c7846ebab7af6e",
                "userid": "e89c3cb72cc95e2e7014d3d07b97beb2",
                "zongliushui": "3500",
                "zongyinkui": "-2006",
                "son": [
                    Array(),
                    Array()
                ]
            }
        ]
    }
]

💫 评论卡

备案号:粤ICP备18155514号-2