日期:2014-05-17 浏览次数:20490 次
<?
$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));
?>