See the Moderator of the infinite classification, divided into a tree structure, how to restore

Source: Internet
Author: User
Parent-child Infinite Classification Primitive array
   Array (' id ' = ' 134 ', ' fid ' = ' 0 ', ' name ' = ' t ', '), 1 = = array (' id ' = ' 133 ', ' FID  ' = = ' 0 ', ' name ' = ' k ', ', ' 2 = = Array ' (' id ' = ' + ', ' fid ' = ' 0 ', ' name ' = ' j ', '), 3 = = Array (' id ' = ' = ' 137 ', ' fid ' = ' 133 ', ' name ' = = ' I am k's Category 1 ',), 4 = = Array (' ID ' =&gt ;    ' 136 ', ' fid ' = ' 134 ', ' name ' and ' = ' I am the classification of T ' T2 ', ', ' 5 = = array (' id ' = ' 135 ', ' fid ' = ' 134 ',  ' Name ' = ' I am the classification of T ' T1 ', ' 6 ' = = Array (' id ' = ' 138 ', ' fid ' = ' 137 ', ' name ' = ' = ' I am the category of K ' 1-1 ', ), 7 = = Array (' id ' = ' = ' 139 ', ' fid ' = ' 138 ', ' name ' = ' I am K ' classification 1-1-1 ', '), 8 = = Array (' id ' = ' + ', ' fid ' = ' 139 ', ' name ' = ' I'm K ' 1-1-1-1 ', '),?> start to form a tree, and increase the path, the tree array is $treesarray ([0] =&G T            Array ([id] = 134 [FID] = 0 [Name] = = [path] = 134 [_sub] =&GT Array ([0] = = Array ([id] = 1                            [FID] = 134 [Name] = I am the category of T T2                            [path] = 134,136) [1] = = Array (                            [id] = 135 [FID] = 134 [name] = = I am the category of T T1             [path] = 134,135)) [1] = = Array ( [id] = 133 [FID] = 0 [name] + k [path] = 133 [_sub  ] = = Array ([0] = = Array ([ID]                            = = 137 [FID] = 133 [Name] = I am the category of K 1 [Path] = 133,137 [_sub] = = Array ([                                            0] = = Array ([id] = 138                                            [FID] = 137 [Name] = I am K's category 1-1 [Path] = 133,137,138 [_sub] = ARR                                                        Ay ([0] = = Array ([ID] =&gt ;                                                            139 [FID] = 138 [Name] = I am K's classification 1-1-1 [path] and 133,137,1                38,139                                            [_sub] = = Array                                                                        ([0] = = Array ([ID] =& Gt                                                                            [FID] = 139                                                                            [Name] = I am the category of K 1-1-1-1                                                                        [Path] = 133,137,138,139,140                                                        )                                                                )                                )                                                )                                        )        )                ))) [2] = = Array ([ID] [FID] = 0 [Name] + j [path] + 132) But I have a question, how to restore the tree is the same as the original array of the two-dimensional form, and then multiple path value, because the main purpose is to add a path The value $a=array (0 = = Array (' id ' = ' 134 ', ' fid ' = ' 0 ', ' name ' = = ' t ', ' path ' = = ' 134 '), 1 =&G  T Array (' id ' = ' 133 ', ' fid ' = ' 0 ', ' name ' = ' k ', ' path ' = ' 133,137 '),........................ . I just want to use recursion unfortunately learned shallow, has not been recursive, can only take a layer ... is not written out function test ($array) {foreach ($array as $k + $v) {if (array_key_exists (' _sub ', $v)) {$temp []=array_slice ($v, 0 , 4); $temp []=test ($v [' _sub ']);} else {$temp []= $v;}} return $temp;}  $x =test ($trees);p rint_r ($x); the printed array has only 3 arrays ([0] = = Array ([id] = 134 [FID] + =            0 [Name] = [path] = 134) [1] = = Array ([ID] + 133 [FID] = 0 [name] =>            K [path] = 133) [2] = = Array ([ID] [FID] = 0 [Name] + j [path] = 132) .... The Code of Genuine request


Reply to discussion (solution)

  Array (' id ' = = 134, ' fid ' = + 0, ' name ' = ' t ', ' path ' = =                            134, ' _sub ' = = Array (0 = = Array ( ' id ' = = 136, ' FID ' and ' 134 ', ' name ' = ' I am the category of T '                        T2 ', ' path ' = ' 134,136 '), 1 = = Array                            (' id ' = = 135, ' fid ' = 134,                 ' Name ' = ' I am the classification of T ' T1 ', ' path ' = ' 134,135 ') ), 1 = = Array (' id ' = = 133, ' fid ' = + 0, ' name ' = = '                        K ', ' path ' = = 133, ' _sub ' = = Array (0 = = Array         (                   ' id ' = = 137, ' fid ' = 133, ' name ' = ' I am                                Classification of K 1 ', ' path ' = ' 133,137 ', ' _sub ' = = Array                                            (0 = Array (                                            ' id ' = = 138, ' fid ' = 137,                                            ' Name ' = ' I am K's classification 1-1 ', ' path ' = ' 133,137,138 ',                                                    ' _sub ' = Array (                                                            0 = Array (                                                            ' id ' = 139,        ' FID ' = 138,                                                    ' Name ' = ' I am the category 1-1-1 of K ', ' Path ' = ' 133,137,138,139 ', ' _sub ' = ' A '                                                                    Rray (                                                                            0 = Array (                                                                            ' id ' = 140,  ' FID ' = 139, ' name ' = ' I am K's classification 1-1-1-1 ', ' path ' = ' 133,137,1                                                              38,139,140 ')   )                                                         )                                                 )     )                                 )                         )                 )         ), 2 = = Array (' id ' = +, ' fid ' = + 0, ' name ' = ' j ', ' P            Ath ' = "), $temp = Array (), function test ($array,& $temp) {foreach ($array as $k + = $v) {                if (array_key_exists (' _sub ', $v)) {$temp []=array_slice ($v, 0,4);             Test ($v [' _sub '], $temp);             } else {Array_push ($temp, $v); }}} Test ($arr, $temp);p Rint_r ($temp);? >

You changed the function to

function Find_child ($ar) {  foreach ($ar as $v) $t [$v [' id ']] = $v;  foreach ($t as $k = + $item) {    $t [$k] [' path '] = $item [' id '];    if ($item [' FID ']) {      $t [$item [' FID ']][' _sub '] [$item [' ID ']] =& $t [$k];      $t [$k] [' path '] = $t [$item [' FID ']][' path ']. ',' . $t [$k] [' id '];    }  }  return $t;}
 $a =array (0 + = array (' id ' = ' 134 ', ' fid ' = ' 0 ', ' name ' = = ' t ', ), 1 = = array (' id ' = ' = ' 133 ', ' fid ' = ' 0 ', ' name ' = ' k ', '), 2 = = Array (' id ' = = ') ", ' fid ' = ' 0 ', ' name ' + ' J ',", 3 = = Array (' id ' = ' = ' 137 ', ' fid ' = ' 133 ', ' name ')   = ' I'm K's Category 1 ', ' 4 = = ' id ' = ' 136 ', ' fid ' = ' 134 ', ' name ' = ' I'm the category of T ' T2 ', 5 ' = Array (' id ' = ' = ' 135 ', ' fid ' = ' 134 ', ' name ' = ' I am ' t ' classification T1 ', '), 6 = = Array (' id ' = ' 138 ') , ' fid ' = ' 137 ', ' name ' and ' = ' I am K's classification 1-1 ', ', ' 7 = = Array (' id ' = ' 139 ', ' fid ' = ' 138 ', ' Name ' = ' I am k's category 1-1-1 ', ', 8 = = Array (' id ' = ' + ', ' fid ' = ' 139 ', ' name ' = ' = ' I am K's category 1-1-1-1 ,), $r = Find_child ($a), foreach ($r as $k + $v) if (Isset ($v [' _sub ')) unset ($r [$k] [' _sub ']);p rint_r ($r); 
Array ([134] = = Array ([id] = 134 [FID] = 0 [name] = = [path] = 134) [133] = = Array ([id] = 133 [FID] + 0 [ Name] + k [path] = 133) [+] = Array ([id] = [FID] ] = 0 [name] + j [path] [137] = = Array ([id] =&G T 137 [FID] = 133 [Name] = = I am k's classification 1 [path] = 133,137) [136] = ARR AY ([id] = = 136 [FID] = 134 [name] = = I am the category of T t2 [path] = 134,136) [135] = = Array ([id] = 135 [FID] = 134 [name] =&gt ;  I am the classification of T T1 [path] = 134,135) [138] + = Array ([id] = 138 [FID]         = 137   [name] = = I am k's classification 1-1 [path] = 133,137,138) [139] = = Array ([id] = 139 [FID] = 138 [name] = = I am the category of K 1-1-1 [path] + 133,137,138,139) [1            +] = Array ([id] = [FID] = 139 [Name] = = I am the category of K 1-1-1-1 [path] = 133,137,138,139,140))

I took the code first, just read it again without reading ...



Thank you two bit, again thanked, points a little less please don't mind


Hey, when can I be as strong as 2 people?

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