Home >Backend Development >PHP Tutorial >php 无限分类如何简化?

php 无限分类如何简化?

WBOY
WBOYOriginal
2016-06-06 20:28:081181browse

数组是
`Array
(

<code>[0] => Array
    (
        [id] => 1
        [pid] => 0
        [name] => 安徽省
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [pid] => 1
                        [name] => 合肥市
                        [son] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 4
                                        [pid] => 3
                                        [name] => 长丰县
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 5
                        [pid] => 1
                        [name] => 安庆市
                    )

            )

    )

[1] => Array
    (
        [id] => 2
        [pid] => 0
        [name] => 浙江省
    )
</code>

)`

然后我把他们列出来 很傻瓜的写法 只能输出三级循环

<code>foreach(上面的数组 as $row) 

 {   echo $row['id']; 

     echo $row['name']; 


  if(isset($row['son'])){

     foreach($row['son'] as $k => $v){ 

         echo $v['id']; 

         echo $v['name']; 

       
         if(isset($v['son'])){
               foreach($v['son'] as $m => $n){ 

                 echo $n['id']; 

                 echo $n['name']; 

               } 
     } 





     } 

   }
 }</code>

这样写效率不好吧?
如何简化写法?或者无限循环。。。

回复内容:

数组是
`Array
(

<code>[0] => Array
    (
        [id] => 1
        [pid] => 0
        [name] => 安徽省
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [pid] => 1
                        [name] => 合肥市
                        [son] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 4
                                        [pid] => 3
                                        [name] => 长丰县
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 5
                        [pid] => 1
                        [name] => 安庆市
                    )

            )

    )

[1] => Array
    (
        [id] => 2
        [pid] => 0
        [name] => 浙江省
    )
</code>

)`

然后我把他们列出来 很傻瓜的写法 只能输出三级循环

<code>foreach(上面的数组 as $row) 

 {   echo $row['id']; 

     echo $row['name']; 


  if(isset($row['son'])){

     foreach($row['son'] as $k => $v){ 

         echo $v['id']; 

         echo $v['name']; 

       
         if(isset($v['son'])){
               foreach($v['son'] as $m => $n){ 

                 echo $n['id']; 

                 echo $n['name']; 

               } 
     } 





     } 

   }
 }</code>

这样写效率不好吧?
如何简化写法?或者无限循环。。。

骚年,凡事先搜索,学会自己解决问题。
5行代码不递归解决无限分类:http://www.oschina.net/code/snippet_173183_11767

不是打广告啊,只是觉得个人总结得还不错:

PHP无限极分类实现【简单版】

您这目前只能输出3级啊,无限级的就需要使用递归了。只能给你来个思路了,具体的百度一下你就知道。

如果是为了代码更易读、易维护,建议使用组合模式,如果只是要高效的输出,@cevin 给出的答案已经足够了

Statement:
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