After reading the unlimited classification of moderators and dividing them into tree structures, how can we restore them?

Source: Internet
Author: User
After reading the unlimited classification of moderators and dividing them into tree structures, how can we restore them?
Parent and child unlimited classification original array
 Array ('id' => '000000', 'fid' => '0', 'name' => 'T ',), 1 => array ('id' => '000000', 'fid' => '0', 'name' => 'K ',), 2 => array ('id' => '000000', 'fid' => '0', 'name' => 'J ',), 3 => array ('id' => '000000', 'fid' => '000000', 'name' => 'Category 1 of k ',), 4 => array ('id' => '000000', 'fid' => '000000', 'name' => 'I am a tid ',), 5 => array ('id' => '000000', 'fid' => '000000', 'name' => 'I am a T1 ',), 6 => array ('id' => '000000', 'fid' => '000000', 'name' => 'I am a k classification 1-1 ',), 7 => array ('id' => '000000', 'fid' => '000000', 'name' => 'Classification of k 1-1-1 ',), 8 => array ('id' => '123', 'fid' => '123 ', 'name' => 'I am a k classification 1-1-1-1-1',),)?> Start to form a tree and add a path, the tree-like Array is $ treesArray ([0] => Array ([id] => 134 [fid] => 0 [name] => t [path] => 134 [_ sub] => Array ([0] => Array ([id] => 136 [fid] => 134 [name] => I am a t classification t2 [path] => 134,136) [1] => Array ([id] => 135 [fid] => 134 [name] => I am a t classification t1 [path] => 134,135 ))) [1] => Array ([id] => 133 [fid] => 0 [name] => k [path] => 133 [_ sub] => Array ([ 0] => Array ([id] => 137 [fid] => 133 [nam E] => I am k classification 1 [path] => 133,137 [_ sub] => Array ([0] => Array ([id] => 138 [fid] => 137 [name] => I am a k classification 1-1 [path] => 133,137,138 [_ sub] => Array ([0] => Array ([id] => 139 [fid] => 138 [name] => I am a k classification 1-1-1 [path] => 133,137,138,139 [_ sub] => Array ([0] => Array ([id] => 140 [fid] => 139 [name] => I am a k classification 1-1-1-1 [path] => 133,137,138,139,140 ))))))))) [2] = & gt; Array ([id] = & gt; 132 [fid] => 0 [name] => j [path] => 132) but I have a question: how can we restore the tree to the two-dimensional form of the original array, then there are multiple path values, because the main purpose is to add a path value $ a = array (0 => array ('id' => '123 ', 'fid' => '0', 'name' => 'T', 'path' => '123 '), 1 => array ('id' => '000000', 'fid' => '0', 'name' => 'K ', 'Path' => '123 '),......................... I only want to use recursion, but it's a pity that I have learned a little less. I have never been recursion, but I can only get one layer... Function test ($ array) {foreach ($ array as $ k => $ v) {if (array_key_exists ('_ sub', $ v )) {$ temp [] = array_slice ($ v,); // $ temp [] = test ($ v [''' _ sub'']);} else {$ temp [] = $ v ;}return $ temp ;}$ x = test ($ trees); print_r ($ x ); the printed Array only has three arrays ([0] => Array ([id] => 134 [fid] => 0 [name] => t [path] => 134) [1] => Array ([id] => 133 [fid] => 0 [name] => k [path] => 133) [2] = & gt; Array ([id] = & gt; 132 [fid] = & gt; 0 [Name] => j [path] => 132 )).... Sincerely ask for code


Reply to discussion (solution)

 Array ('id' => 134, 'fid' => 0, 'name' => 'T', 'path' => 134, '_ sub' => Array (0 => Array ('id' => 136, 'fid' => 134, 'name' => 'I Am t's classification t2', 'path' => '123'), 1 => Array ('id' => 134,136, 'fid' => 134, 'name' => 'class T1', 'path' => '123 '))), 1 => Array ('id' => 133, 'fid' => 0, 'name' => 'K', 'path' => 133, '_ sub' => Array (0 => Array ('id' => 137, 'fid' => 133, 'name' => 'I am k's classification 1 ', 'Path' => '123', '_ sub' => Array (0 => Array ('id' => 133,137, 'fid' => 138, 'name' => 'I am k classification 1-1', 'path' => '123 ', '_ sub' => Array (0 => Array ('id' => 139, 'fid' => 138, 'name' => 'I am a k classification 1-1-1', 'path' => '123 ', '_ sub' => Array (0 => Array ('id' => 140, 'fid' => 139, 'name' => 'I am a k classification 1-1-1-1', 'path' => '123 '))))))))), 2 = & gt; Array ('id' = & gt; 132, 'Fid' => 0, 'name' => 'J', 'path' => 132); $ temp = array (); function test ($ array, & $ temp) {foreach ($ array as $ k => $ v) {if (array_key_exists ('_ sub', $ v )) {$ temp [] = array_slice ($ v,); test ($ v ['_ sub'], $ temp);} else {array_push ($ temp, $ v) ;}} test ($ arr, $ temp); print_r ($ temp);?>

You change the function

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' => '000000', 'fid' => '0', 'name' => 'T ',), 1 => array ('id' => '000000', 'fid' => '0', 'name' => 'K ',), 2 => array ('id' => '000000', 'fid' => '0', 'name' => 'J ',), 3 => array ('id' => '000000', 'fid' => '000000', 'name' => 'Category 1 of k ',), 4 => array ('id' => '000000', 'fid' => '000000', 'name' => 'I am a tid ',), 5 => array ('id' => '000000', 'fid' => '000000', 'name' => 'I am a T1 ',), 6 => array ('id' => '000000', 'fid' => '000000', 'name' => 'I am k 1-1 1-1 ',), 7 => array ('id' => '000000', 'fid' => '000000', 'name' => 'Classification of k 1-1-1 ',), 8 => array ('id' => '123', 'fid' => '123 ', 'name' => 'I am a k classification 1-1-1-1-1',),); $ r = find_child ($ a); foreach ($ r as $ k => $ v) if (isset ($ v ['_ sub']) unset ($ r [$ k] ['_ sub']); print_r ($ r );
Array ([134] => Array ([id] => 134 [fid] => 0 [name] => t [path] => 134) [133] => Array ([id] => 133 [fid] => 0 [name] => k [path] => 133) [132] => Array ([id] => 132 [fid] => 0 [name] => j [path] => 132) [137] => Array ([id] => 137 [fid] => 133 [name] => I am a k classification 1 [path] => 133,137) [136] => Array ([id] => 136 [fid] => 134 [name] => I am a t classification t2 [path] => 134,136) [135] => Array ([id] => 135 [fid] => 134 [name] => I am a t classification t1 [path] => 134,135) [138] => Array ([id] => 138 [fid] => 137 [name] => I am a k classification 1-1 [path] => 133,137,138) [139] => Array ([id] => 139 [fid] => 138 [name] => I am a k classification 1-1-1 [path] => 133,137,138,139) [140] => Array ([id] => 140 [fid] => 139 [name] => I am a k classification 1-1-1-1 [path] => 133,137,138,139,140 ))

I accept the code first. I did not understand it after reading it again ......



Thank you! Thank you again. please do not mind


When can I be as strong as a 2-digit player?

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.