Html-php reading classification generates two-dimensional array with duplicate values

Source: Internet
Author: User
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);

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.