php递归函数--遍历

多应用在 栏目多级、无限极调用

数据库数据如:

id   catname   pid

1    关于我们     0

2     新闻          0

3    公司新闻     2

4     行业动态    2

select读出数据后整理函数 $cate
function digui($cate,$pid=0){

  foreach($cate as $v){

    if($v['pid']==$pid){

      $v['child'] = digui($cate,$v['id']);

      $arr[] = $v;
    }
  }

return $arr;

}

相当于不断在循环整个$cate数组。。

遍历html代码

 1          <foreach name="cate" item='app'>
 2             <div class="app">
 3                 <p>
 4                     <strong>{$app.catname}</strong>
 8                 </p>
 9                 <foreach name="app.child" item='action'>
10                     <dl>
11                         <dt>
12                             <strong>{$action.catnaem}</strong>
16                         </dt>
24         </dl>
25           </foreach>
26         </div>
27       </foreach>
原文地址:https://www.cnblogs.com/lola/p/5677082.html