Home  >  Article  >  Backend Development  >  php 按pid排序并给出深度

php 按pid排序并给出深度

WBOY
WBOYOriginal
2016-06-06 20:12:401692browse

<code>$arr=[
    [
        'id'=>1,
        'pid'=>0,
        'name'=>'name1',
    ],
    [
        'id'=>2,
        'pid'=>0,
        'name'=>'name2',
    ],
    [
        'id'=>3,
        'pid'=>0,
        'name'=>'name3',
    ],
    [
        'id'=>4,
        'pid'=>1,
        'name'=>'name4',
    ],
    [
        'id'=>5,
        'pid'=>4,
        'name'=>'name5',
    ],
    [
        'id'=>6,
        'pid'=>2,
        'name'=>'name6',
    ],
    [
        'id'=>7,
        'pid'=>2,
        'name'=>'name7',
    ],
];
</code>

排序过会得出

<code>$result=[
    [
        'id'=>1,
        'pid'=>0,
        'name'=>'name1',
        'level'=>1,
    ],
    [
        'id'=>4,
        'pid'=>1,
        'name'=>'name4',
        'level'=>2,
    ],
    [
        'id'=>5,
        'pid'=>4,
        'name'=>'name5',
        'level'=>3,
    ],
    [
        'id'=>2,
        'pid'=>0,
        'name'=>'name2',
        'level'=>1,
    ],
    [
        'id'=>7,
        'pid'=>2,
        'name'=>'name7',
        'level'=>'2',
    ],
    [
        'id'=>6,
        'pid'=>2,
        'name'=>'name6',
        'level'=>2,
    ]
    [
        'id'=>3,
        'pid'=>0,
        'name'=>'name3',
        'level'=>1,
    ],   
];</code>

回复内容:

<code>$arr=[
    [
        'id'=>1,
        'pid'=>0,
        'name'=>'name1',
    ],
    [
        'id'=>2,
        'pid'=>0,
        'name'=>'name2',
    ],
    [
        'id'=>3,
        'pid'=>0,
        'name'=>'name3',
    ],
    [
        'id'=>4,
        'pid'=>1,
        'name'=>'name4',
    ],
    [
        'id'=>5,
        'pid'=>4,
        'name'=>'name5',
    ],
    [
        'id'=>6,
        'pid'=>2,
        'name'=>'name6',
    ],
    [
        'id'=>7,
        'pid'=>2,
        'name'=>'name7',
    ],
];
</code>

排序过会得出

<code>$result=[
    [
        'id'=>1,
        'pid'=>0,
        'name'=>'name1',
        'level'=>1,
    ],
    [
        'id'=>4,
        'pid'=>1,
        'name'=>'name4',
        'level'=>2,
    ],
    [
        'id'=>5,
        'pid'=>4,
        'name'=>'name5',
        'level'=>3,
    ],
    [
        'id'=>2,
        'pid'=>0,
        'name'=>'name2',
        'level'=>1,
    ],
    [
        'id'=>7,
        'pid'=>2,
        'name'=>'name7',
        'level'=>'2',
    ],
    [
        'id'=>6,
        'pid'=>2,
        'name'=>'name6',
        'level'=>2,
    ]
    [
        'id'=>3,
        'pid'=>0,
        'name'=>'name3',
        'level'=>1,
    ],   
];</code>

<code>function treelist($data, $pid, $deep = 1)
{
    static $tree = array();
    foreach ($data as $row) {
        if ($row ['pid'] == $pid) {
            $row ['lever'] = $deep;
            $tree [] = $row;
            treelist($data, $row ['id'], $deep + 1);
        }
    }
    return $tree;
}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn