How does an array like this make its tree-like display?

Source: Internet
Author: User
Tree-like infinite classification

Array ([    1] = = Array        (            [id] = 1            [name] = + World            [PID] + 0            [2] = = Array                (                    [ ID] + 2                    [name] + China                    [PID] + 1                    [4] = = Array                        (                            [id] + 4                            [name]                            = + Hunan [PID] + 2                        )                )            [3] = = Array                (                    [id] + 3                    [name] = + US                    [pid] + 1                )        ))


Reply to discussion (solution)

itself is a tree-like, PID is not simple? Just a recursive line.

$a = Array (  1 = = Array (    ' id ' = = 1,    ' name ' = ' World ',    ' pid ' = = 0,    2 = = Array (      ' id ' =&G T 2,      ' name ' = ' China ',      ' pid ' = + 1,      4 = = Array (        ' id ' = = 4,        ' name ' = ' hu ') ', '        pid ' => ; 2,      ),    ),    3 = = Array (      ' id ' = + 3,      ' name ' = ' Us ',      ' pid ' = 1,  '),) ; tree ($a); function tree ($ar) {  if (! Is_array ($ar)) return;  Echo '
 
  
  
    ; foreach ($ar as $item) { echo "$item [id] $item [name]"; foreach ($item as $t) { if (Is_array ($t)) { tree (Array ($t)) }}} Echo '
;}
1 World 2 China 4 Hunan 3 USA

 
  
  
    1 World
      2 China
        4 hunan
      3 USA

I'll give you an interesting play (I don't know what to do with it)

$a = Array (  1 = = Array (    //' id ' = + 1,    ' name ' = = ' world ',    //' pid ' = 0,    2 = = Array (      // ' id ' = 2,      ' name ' = ' China ', '/      ' pid ' = + 1,      4 = = Array (        //' id ' = + 4,        ' name ' = ' Hunan ') ,        //' pid ' = 2,      ),    3 = = Array (      //' id ' = + 3,      ' name ' = ' US ',      //' pid ' = > 1,),)  , $awesome = new Recursivetreeiterator (      new Recursivearrayiterator ($a),      null, NULL, Recursiveiteratoriterator::leaves_only  );  The echo $line of the foreach ($awesome as $line)      . Php_eol;  

  |-World | |-  China  |   \-Hunan    \-USA

Grey often thanks! Cry!! It's been a few days,,,

  • 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.