php数组递归方法多个实例
时间:2021-07-01 10:21:17
帮助过:25人阅读
php数组递归方法
有如下php数组:
- function genTree5($items) {
- foreach ($items as $item)
- $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
- return isset($items[0]['son']) ? $items[0]['son'] : array();
- }
方法二:
function findChild($arr,$id){ - $childs=array();
- foreach ($arr as $k => $v){
- if($v['pid']== $id){
- $childs[]=$v;
- }
- }
- // echo "
";print_r($childs);die(); - return $childs;
- }
function build_tree($root_id){ - global $items;
- $childs =array();
- $childs=findChild($items,$root_id);
- // print_r($childs);
- // die();
- if(empty($childs)){
- return null;
- }
- foreach ($childs as $k => $v){
- $rescurTree=build_tree($v['id']);
- if( null != $rescurTree){
- $childs[$k]['son']=$rescurTree;
- }
- }
- return $childs;
- }
|