时间:2021-07-01 10:21:17 帮助过:20人阅读
$arr = array(
array('id'=>1,'city_name'=>'中国','rel_id'=>'1','pid'=>0),
array('id'=>2,'city_name'=>'广东','rel_id'=>'1-2','pid'=>1),
array('id'=>3,'city_name'=>'深圳','rel_id'=>'1-2-3','pid'=>2),
array('id'=>4,'city_name'=>'广州','rel_id'=>'1-2-4','pid'=>2)
);
function find_subclass( $pid ){
global $arr;
$__arr = array();
foreach ( $arr as $k=>$v )
{
if( $v['pid']==$pid )$__arr[] = $v;
}
return $__arr;
}
function tree_subclass($pid=0){
$__arr = array();
$__arr = find_subclass($pid);
if( !empty($__arr) ){
foreach ( $__arr as $k=>$v )
{
$__arr[$k]['subclass'] = tree_subclass($v['id']);
}
}
return $__arr;
}
var_dump(tree_subclass(0));
?>
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["city_name"]=>
string(4) "中国"
["rel_id"]=>
string(1) "1"
["pid"]=>
int(0)
["subclass"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
int(2)
["city_name"]=>
string(4) "广东"
["rel_id"]=>
string(3) "1-2"
["pid"]=>
int(1)
["subclass"]=>
array(2) {
[0]=>
array(5) {
["id"]=>