PHP中不用递归实现无限极分类

在实际工作中,一个递归方法被重复调用的话,如果使用的static方式,由于上一次的数据并不会消失,第二次调用会造成数据的混乱。所以出现这种情况,需要换一种方式来解决。这里用的&引用传递来代替上一种方法。

下面是网上的一个例子,贴出来大家看下:

$items  array (
     1 =>  array ( 'id'  => 1,  'pid'  => 0,  'name'  =>  '江西省' ),
     2 =>  array ( 'id'  => 2,  'pid'  => 0,  'name'  =>  '黑龙江省' ),
     3 =>  array ( 'id'  => 3,  'pid'  => 1,  'name'  =>  '南昌市' ),
     4 =>  array ( 'id'  => 4,  'pid'  => 2,  'name'  =>  '哈尔滨市' ),
     5 =>  array ( 'id'  => 5,  'pid'  => 2,  'name'  =>  '鸡西市' ),
     6 =>  array ( 'id'  => 6,  'pid'  => 4,  'name'  =>  '香坊区' ),
     7 =>  array ( 'id'  => 7,  'pid'  => 4,  'name'  =>  '南岗区' ),
     8 =>  array ( 'id'  => 8,  'pid'  => 6,  'name'  =>  '和兴路' ),
     9 =>  array ( 'id'  => 9,  'pid'  => 7,  'name'  =>  '西大直街' ),
     10 =>  array ( 'id'  => 10,  'pid'  => 8,  'name'  =>  '东北林业大学' ),
     11 =>  array ( 'id'  => 11,  'pid'  => 9,  'name'  =>  '哈尔滨工业大学' ),
     12 =>  array ( 'id'  => 12,  'pid'  => 8,  'name'  =>  '哈尔滨师范大学' ),
     13 =>  array ( 'id'  => 13,  'pid'  => 1,  'name'  =>  '赣州市' ),
     14 =>  array ( 'id'  => 14,  'pid'  => 13,  'name'  =>  '赣县' ),
     15 =>  array ( 'id'  => 15,  'pid'  => 13,  'name'  =>  '于都县' ),
     16 =>  array ( 'id'  => 16,  'pid'  => 14,  'name'  =>  '茅店镇' ),
     17 =>  array ( 'id'  => 17,  'pid'  => 14,  'name'  =>  '大田乡' ),
     18 =>  array ( 'id'  => 18,  'pid'  => 16,  'name'  =>  '义源村' ),
     19 =>  array ( 'id'  => 19,  'pid'  => 16,  'name'  =>  '上坝村' ),
);
function  genTree( $items ) {
     $tree  array ();  //格式化好的树
     foreach  ( $items  as  $item )
         if  (isset( $items [ $item [ 'pid' ]]))
             $items [ $item [ 'pid' ]][ 'son' ][] = & $items [ $item [ 'id' ]];
         else
             $tree [] = & $items [ $item [ 'id' ]];
     return  $tree ;
}

打印结果,太长了,只贴一部分。

Array
(
[0] => Array
     (
         [id] => 1
         [pid] => 0
         [name] => 江西省
         [son] => Array
             (
                 [0] => Array
                     (
                         [id] => 3
                         [pid] => 1
                         [name] => 南昌市
                     )
  
                 [1] => Array
                     (
                         [id] => 13
                         [pid] => 1
                         [name] => 赣州市
                         [son] => Array
                             (
                                 [0] => Array
                                     (
                                         [id] => 14
                                         [pid] => 13
                                         [name] => 赣县
                                         [son] => Array
                                             (
                                             [0] => Array
                                                 (
                                                     [id] => 16
                                                     [pid] => 14
                                                     [name] => 茅店镇
                                                     [son] => Array
                                                         (
                                                         [0] => Array
                                                             (
                                                             [id] => 18
                                                             [pid] => 16
                                                             [name] => 义源村
                                                             )
  
                                                         [1] => Array
                                                             (
                                                             [id] => 19
                                                             [pid] => 16
                                                             [name] => 上坝村
                                                             )
  
                                                         )
  
                                                 )
  
                                             [1] => Array
                                                 (
                                                     [id] => 17
                                                     [pid] => 14
                                                     [name] => 大田乡
                                                 )
  
                                             )
  
                                     )
  
                                 [1] => Array
                                     (
                                         [id] => 15
                                         [pid] => 13
                                         [name] => 于都县
                                     )
  
                             )
  
                     )
  
             )
  
     )
  
[1] => Array
     (
  
     )
)*/

第一次看到这个方法时我没看懂,今天回答贴子的问题时,也尝试了用&,才明白

&符号:$a=&$b  , $b变量的内存地址与$a变量的内存地址,指向了同一个地方.

这样,已经生成的数组,会随着后面值的变化不断变化,比如$arr['son'] = &$array,  虽然此次循环已经完毕,但是$arr['son'] 指向了$array同一个地方,再后面的循环中,会随着$array值的变化,随时随地的变化。由此顶级分类不断的壮大,最终组装成想要的结构。形成了数据的同步。

为了更好的说明&的作用。下面是我刚作的一个例子。

$data[] = array('id'=>"1601");
$data[] = array('id'=>"160101");
$data[] = array('id'=>"160102");
$data[] = array('id'=>"16010201");
$data[] = array('id'=>"1601020101");
$data[] = array('id'=>"1601020102");
$data[] = array('id'=>"160103");

$data[] = array('id'=>"16010301");

foreach ($data as $k => $v) {
$arr[ $v['id'] ] = $v;
}
foreach ($arr as $k => &$v) {
for($i=1;$i<100;$i++)
{
$oo = $i<10?'0'.$i:$i;
$sub_k = $k.$oo;
if( isset( $arr[$sub_k] ) )
{
$v['child'][$sub_k] = &$arr[$sub_k];//这里使用了&,数据实时同步。
$temp[] = $sub_k;
}
}

}

//如测试&注释掉下面的

/*for($i=0;$i unset($arr[$temp[$i]]);

}*/  

如果不用&,会啥样呢?

Array
(
    [1601] => Array
        (
            [id] => 1601
            [child] => Array
                (
                    [160101] => Array
                        (
                            [id] => 160101
                        )

                    [160102] => Array
                        (
                            [id] => 160102
                        )

                    [160103] => Array
                        (
                            [id] => 160103
                        )

                )

        )

    [160101] => Array
        (
            [id] => 160101
        )

    [160102] => Array
        (
            [id] => 160102
            [child] => Array
                (
                    [16010201] => Array
                        (
                            [id] => 16010201
                        )

                )

        )

    [16010201] => Array
        (
            [id] => 16010201
            [child] => Array
                (
                    [1601020101] => Array
                        (
                            [id] => 1601020101
                        )

                    [1601020102] => Array
                        (
                            [id] => 1601020102
                        )

                )

        )

    [1601020101] => Array
        (
            [id] => 1601020101
        )

    [1601020102] => Array
        (
            [id] => 1601020102
        )

    [160103] => Array
        (
            [id] => 160103
            [child] => Array
                (
                    [16010301] => Array
                        (
                            [id] => 16010301
                        )

                )

        )

    [16010301] => Array
        (
            [id] => 16010301
        )

)

加上&:

Array
(
    [1601] => Array
        (
            [id] => 1601
            [child] => Array
                (
                    [160101] => Array
                        (
                            [id] => 160101
                        )

                    [160102] => Array
                        (
                            [id] => 160102
                            [child] => Array
                                (
                                    [16010201] => Array
                                        (
                                            [id] => 16010201
                                            [child] => Array
                                                (
                                                    [1601020101] => Array
                                                        (
                                                            [id] => 1601020101
                                                        )

                                                    [1601020102] => Array
                                                        (
                                                            [id] => 1601020102
                                                        )

                                                )

                                        )

                                )

                        )

                    [160103] => Array
                        (
                            [id] => 160103
                            [child] => Array
                                (
                                    [16010301] => Array
                                        (
                                            [id] => 16010301
                                        )

                                )

                        )

                )

        )

    [160101] => Array
        (
            [id] => 160101
        )

    [160102] => Array
        (
            [id] => 160102
            [child] => Array
                (
                    [16010201] => Array
                        (
                            [id] => 16010201
                            [child] => Array
                                (
                                    [1601020101] => Array
                                        (
                                            [id] => 1601020101
                                        )

                                    [1601020102] => Array
                                        (
                                            [id] => 1601020102
                                        )

                                )

                        )

                )

        )

    [16010201] => Array
        (
            [id] => 16010201
            [child] => Array
                (
                    [1601020101] => Array
                        (
                            [id] => 1601020101
                        )

                    [1601020102] => Array
                        (
                            [id] => 1601020102
                        )

                )

        )

    [1601020101] => Array
        (
            [id] => 1601020101
        )

    [1601020102] => Array
        (
            [id] => 1601020102
        )

    [160103] => Array
        (
            [id] => 160103
            [child] => Array
                (
                    [16010301] => Array
                        (
                            [id] => 16010301
                        )

                )

        )

    [16010301] => Array
        (
            [id] => 16010301
        )

)

发现,加上&后,

PHP中不用递归实现无限极分类_第1张图片

   结果:

PHP中不用递归实现无限极分类_第2张图片

这样就通过引用数据同步的原理,同样实现了递归的效果,而且引用的效率要比递归高。

有兴趣的朋友可以,拷贝下来,去掉&,看看是什么结果。加上&又是什么结果。

end;

你可能感兴趣的:(代码实战)