Recursive infinite Classification in PHP-Find the parent column by sub-column

Source: Internet
Author: User

<?phpheader ("Content-type:text/html;charset=utf8");/* Through the sub-column to find the parent column, the range is small to large range */$area  = array (Array (' Cat _id ' =>1, ' province ' = ' Fujian ', ' parent_id ' =>0 ', Array (' cat_id ' =>2, ' province ' = ' Longyan ', ' parent_id ' =>1 ), Array (' cat_id ' =>3, ' province ' = ' Guangdong province ', ' parent_id ' =>0), array (' cat_id ' =>4, ' province ' = ' Foshan ', ' parent_id ' =>3), array (' cat_id ' =>5, ' province ' = ' Jiangxi province ', ' parent_id ' =>0), array (' cat_id ' =>6, ' province ' = ' Nanchang ', ' parent_id ' =>5), array (' cat_id ' =>7, ' province ' = ' Ganzhou ', ' parent_id ' =>5), array (' cat_id ' = = 8, ' province ' = ' Ningdou County ', ' parent_id ' =>7), Function son ($arr, $cat _id) {static  $list; foreach ($arr  as  $v) {if ($v [' cat_id '] ==  $cat _id) {print_r ($v); $list [] = $v; son ($arr, $v [' parent_id ']) ;}} return  $list;} $list  = son ($area, 8), foreach ($list  as  $v) {echo  '---> ', $v [' Province '];} /** Execution Result source code     Array     (         [cat_id] => 8        [province] =>  Ningdou County          [parent_id] => 7    )      Array     (        [cat_id] => 7         [province] =>  Ganzhou          [parent_id] => 5    )     Array      (        [cat_id] => 5         [province] =>  Jiangxi Province          [parent_id] => 0    )     ---> Ningdou County---> Ganzhou--- > **/?>, Jiangxi Province

This article is from the "Small City Studio" blog, please be sure to keep this source http://xcroom.blog.51cto.com/7941996/1650600

Recursive infinite Classification in PHP-Find the parent column by sub-column

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.