Home > Backend Development > PHP Tutorial > html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-09-11 11:34:12
Original
1113 people have browsed it

Read the category directory to generate a two-dimensional array under the top category that contains other categories under the top level
But the two-dimensional array under the next top-level category directory will contain the value of the previous top-level directory. It seems to be the reason for static declaration of variables, but I don’t know how. It’s the same after changing the release. Below is a screenshot to ask for a reason:
html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

Picture description

html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

Reply content:

Read the category directory to generate a two-dimensional array under the top category that contains other categories under the top level
But the two-dimensional array under the next top-level category directory will contain the value of the previous top-level directory. It seems to be the reason for static declaration of variables, but I don’t know how. It’s the same after changing the release. Below is a screenshot to ask for a reason:
html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

Picture description

html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

html - PHP reads classification and generates two-dimensional array. Two-dimensional array values ​​are repeated.

I searched again and the following one should meet the requirements.

<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);

</code>
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template