<?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