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

php 按pid排序并给出深度

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:12:40
Original
1748 people have browsed it

<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>
Copy after login
Copy after login

排序过会得出

<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>
Copy after login
Copy after login

回复内容:

<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>
Copy after login
Copy after login

排序过会得出

<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>
Copy after login
Copy after login

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template