PHP Simple implementation of infinite Classification tree list method _php Tips

Source: Internet
Author: User

This article illustrates a simple way for PHP to implement an infinitely sorted tree list. Share to everyone for your reference. as follows:

$items = Array (1 => array (' ID ' => 1, ' pid ' => 0, ' name ' => ' Jiangxi Province '), 2 => array (' ID ' => 2, ' pid ' = > 0, ' name ' => ' Heilongjiang province ', 3 => array (' ID ' => 3, ' pid ' => 1, ' name ' => ' Nanchang '), 4 => array (' ID ' =&gt ; 4, ' PID ' => 2, ' name ' => ' Harbin '), 5 => array (' ID ' => 5, ' pid ' => 2, ' name ' => ' ' Jixi '), 6 => array  (' ID ' => 6, ' PID ' => 4, ' name ' => ' Xiangfang District '), 7 => array (' ID ' => 7, ' PID ' => 4, ' name ' => ' Nangang '), 8 => Array (' ID ' => 8, ' pid ' => 6, ' name ' => ' and Hing Road '), 9 => array (' ID ' => 9, ' PID ' => 7, ' name ' => ' West big Straight Street '), => array (' ID ' =>, ' pid ' => 8, ' name ' => ' Nefu '), one => array (' ID ' => one, ' pid ' =&G T 9, ' name ' => ' Harbin Institute of Technology '), => array (' ID ' =>, ' pid ' => 8, ' name ' => ' Harbin Normal University '), => array (' ID ' =>, ' pid ' => 1, ' name ' => ' Ganzhou '), => array (' ID ' =>, ' pid ' => ', ' name ' => ' Gan County '), 15 = > Array (' ID ' =&GT  , ' pid ' =>, ' name ' => ' Yudu '), => array (' ID ' =>, ' pid ' =>, ' name ' => ' Mao Dian Town '), => Array (' ID ' =>, ' pid ' =>, ' name ' => ' da Tian Xiang '), => array (' ID ' =>, ' pid ' =>, ' name ' => ') => Array (' ID ' =>, ' pid ' =>, ' name ' => ' Shang Cun '), => array (' ID ' =>, ' pid ' => 
0, ' name ' => ' Guangdong province '), => array (' ID ' =>, ' pid ' =>, ' name ' => ' Guangzhou '),); 
$t = Array (); foreach ($items as $id => $item) {if ($item [' pid ']) {$items [$item [' PID ']][$item [' id ']] = & $items [$item [' I]
    d ']]; 
  $t [] = $id; 
foreach ($t as $u) {unset ($items [$u]); 
echo "<pre>"; Print_r ($items);

I hope this article will help you with your PHP programming.

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.