1,'pid'=>0,'name'=>'中国') ,array('id'=>5,'pid'=>0,'name'=>'美国') ,array('id'=>2,'pid'=>1,'name'=>'吉林') ,array('id'=>4,'pid'=>2,'name'=>'长春') ,array('id'=>3,'pid'=>5,'name'=>'墨西哥') ); $tmparr = array(); foreach ($area as $value) { $tmparr\[$value\['id'\]\] = $value; } function gen\_tree($data) { $tree = array(); foreach ($data as $key => $value) { if(isset($data\[$value\['pid'\]\])) { $data\[$value\['pid'\]\]\['child'\]\[\] = &$data\[$value\['id'\]\]; } else { $tree\[\] = &$data\[$value\['id'\]\]; } } return $tree; } print\_r(gen\_tree($tmparr));
PHP创建多级树型结构
点赞
收藏