PHP Sort by PID and give depth

Source: Internet
Author: User
Keywords Php
$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',    ],];

Sort over will draw

$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,    ],   ];

Reply content:

$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',    ],];

Sort over will draw

$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,    ],   ];

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;}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.