Read the category directory to generate a two-dimensional array containing other categories under the top-level category. However, the two-dimensional array under the next top-level category directory will contain the previous top-level value, which seems to be the reason for the static declarative variable, I don't know how to release it. The following is a reason :...
Read the category directory to generate a two-dimensional array containing top-level other categories
However, the two-dimensional array under the next top-level category directory will contain the last top-level value, which seems to be the reason for static variable declaration, but I don't know how to release it. The following is a reason:
Image description
Reply content:
Read the category directory to generate a two-dimensional array containing top-level other categories
However, the two-dimensional array under the next top-level category directory will contain the last top-level value, which seems to be the reason for static variable declaration, but I don't know how to release it. The following is a reason:
Image description
After searching again, we should be able to meet the requirements below.
Array ('id' => 1, 'pid '=> 0,), '2' => array ('id' => 2, 'pid '=> 0,), '3' => array ('id' => 3, 'pid' => 1 ,), '4' => array ('id' => 4, 'pid '=> 2,), '5' => array ('id' => 5, 'pid '=> 4), '6' => array ('id' => 6, 'pid' => 5 ,), '7' => array ('id' => 7, 'pid '=> 3), '8' => array ('id' => 8, 'pid '=> 4,), '9' => array ('id' => 9, 'pid' => 7 ),); // format foreach ($ arr as $ key => $ value) with ID as the key value {$ list [$ value ['id'] = $ value ;} // traverse the array foreach ($ list as $ key => $ value) {if ($ Value ['pid '] = 0) {$ tree [$ value ['id'] = $ value;} else {// ancestor id $ ancestor_id = ''; // determine whether there is a corresponding value in the ancestor array. if yes, retrieve if (! Empty ($ ancestor [$ value ['pid ']) {$ ancestor_id = $ ancestor [$ value ['pid'];} else {$ topid = $ value ['id']; $ child = array (); if (empty ($ ancestor [$ topid]) {$ child [] = $ topid;} // find their fathers through a loop layer by layer, until the ancestor while ($ list [$ topid] ['pid ']> 0) {$ topid = $ list [$ topid] ['pid']; if (empty ($ topid) {$ topid = ''; break;} else {// No in the ancestor array, then put the child array if (empty ($ ancestor [$ topid]) {$ child [] = $ topid;} else {// The ancestor already exists, no need to loop down $ t Opid = $ ancestor [$ topid]; break ;}}$ ancestor_id = $ topid; // if the ancestor is found, add the descendant array to the ancestor array if (! Empty ($ ancestor_id) {foreach ($ child as $ k = >$ v) {$ ancestor [$ v] = $ ancestor_id ;}} if (! Empty ($ ancestor_id) {$ tree [$ ancestor_id] ['child '] [$ value ['id'] = $ value ;}} echo'';var_dump($tree);