分类 算法 下的文章

根据分类实现无限级

数组

$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()
                ]
            }
        ]
    }
]

阅读

百钱买百鸡

公鷄5元一隻,母鷄2元一隻,小鷄1元3只,100元可以買100只,求公鷄、母鷄、小鷄共多少隻

分析:假设有$i只鸡,$j只母鸡,$k只小鸡,并且$i+$j+$k的总数为100,即$i*5+$j*3+$k/3=100($k必须是3的倍数)

$count = 100;
function countNum($count) {
    for ($i = 1; $i < $count; $i++) {
        for ($j = 1; $j < $count; $j++) {
            for ($k = 3; $k < $count; $k = $k+3) {
                if(($i+$j+$k==$count) && ($i*5+$j*3+$k/3==$count)){
                    var_dump("公鷄:{$i}只,母鷄:{$j}只,小鷄:{$k}只");
                }
            }
        }
    }
}

简单实现无限极

<?php
function generateTree($array)
{
    //第一步 构造数据
    $items = array();
    foreach ($array as $value) {
        $items[$value['id']] = $value;
    }
    //第二步 遍历数据 生成树状结构
    $tree = array();
    foreach ($items as $key => $item) {
        if (isset($items[$item['pid']])) {
            $items[$item['pid']]['son'][] = &$items[$key];
        } else {
            $tree[] = &$items[$key];
        }
    }
    return $tree;
}
$array = [
    [
        'id' => 1,
        'pid' => 0,
    ],
    [
        'id' => 2,
        'pid' => 1,
    ],
    [
        'id' => 3,
        'pid' => 2,
    ],
    [
        'id' => 4,
        'pid' => 3,
    ],
    [
        'id' => 5,
        'pid' => 4,
    ],
    [
        'id' => 6,
        'pid' => 5,
    ],
    [
        'id' => 7,
        'pid' => 6,
    ],
    [
        'id' => 8,
        'pid' => 7,
    ],
    [
        'id' => 9,
        'pid' => 8,
    ],
    [
        'id' => 10,
        'pid' => 9,
    ],
];
$getTree = generateTree($array);
print_r($getTree);
备案号:粤ICP备18155514号-2