Heim  >  Artikel  >  Backend-Entwicklung  >  html - php读取分类 生成二维数组 二维数组值重复

html - php读取分类 生成二维数组 二维数组值重复

WBOY
WBOYOriginal
2016-09-11 11:34:121011Durchsuche

读取分类目录 生成顶级分类下包含顶级下的其他分类的二维数组
但是下一个顶级分类目录下的二维数组会包含上一个顶级的的值 好像是static申明变量的原因 但是不知道怎么改 释放了也一样 下面是截图 求一个原因:
html - php读取分类 生成二维数组 二维数组值重复

图片描述

html - php读取分类 生成二维数组 二维数组值重复

html - php读取分类 生成二维数组 二维数组值重复

回复内容:

读取分类目录 生成顶级分类下包含顶级下的其他分类的二维数组
但是下一个顶级分类目录下的二维数组会包含上一个顶级的的值 好像是static申明变量的原因 但是不知道怎么改 释放了也一样 下面是截图 求一个原因:
html - php读取分类 生成二维数组 二维数组值重复

图片描述

html - php读取分类 生成二维数组 二维数组值重复

html - php读取分类 生成二维数组 二维数组值重复

重新找了下,下面这个应该能符合要求。

<code><?php $arr= array(
                '1'=>array('id'=>1, 'pid'=>0,),
                '2'=>array('id'=>2, 'pid'=>0,),
                '3'=>array('id'=>3, 'pid'=>1,),
                '4'=>array('id'=>4, 'pid'=>2,),
                '5'=>array('id'=>5, 'pid'=>4),
                '6'=>array('id'=>6, 'pid'=>5,),
                '7'=>array('id'=>7, 'pid'=>3),
                '8'=>array('id'=>8, 'pid'=>4,),
                '9'=>array('id'=>9, 'pid'=>7),
        );


//以ID为键值格式化
foreach ($arr as $key => $value) {
    $list[$value['id']]=$value;
}

//遍历数组
foreach ($list as $key => $value) {
    if($value['pid']==0){
        $tree[$value['id']]=$value;

    }else{
        //祖先ID
        $ancestor_id='';
        //判断祖先数组里面有没有对应的值,有则取出
        if(!empty($ancestor[$value['pid']])){
            $ancestor_id=$ancestor[$value['pid']];

        }else{
        
            $topid=$value['id'];
            $child=array();
            if(empty($ancestor[$topid])){
                $child[]=$topid;
            }
            //通过循环,一层一层的往上找,找出他们各自的父亲,直到祖宗为止
            while($list[$topid]['pid'] > 0)
            {
                
                    
                    $topid = $list[$topid]['pid'];    

                    if(empty($topid)){
                        $topid='';
                        break;

                    }else{

                        //祖宗数组里面没有,则放入子孙数组
                        if(empty($ancestor[$topid])){
                            $child[]=$topid;
                        }else{
                            //祖先已经存在,就不用循环下去了
                            $topid=$ancestor[$topid];
                            break;

                        }
                        
                    }
            
                    
            }
        
            $ancestor_id= $topid;
            //如果找到祖宗,就将子孙数组对应加入到祖宗数组里面
            if(!empty($ancestor_id)){
                foreach ($child as $k => $v) {
                    $ancestor[$v]=$ancestor_id;
                }
                

            }
            
        }
        if(!empty($ancestor_id)){
            $tree[$ancestor_id]['child'][$value['id']]=$value;

        }


        
    }


    
}
echo '<pre class="brush:php;toolbar:false">';
var_dump($tree);

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn