php递归函数--遍历

时间:2023-03-09 12:53:25
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代码

          <foreach name="cate" item='app'>
<div class="app">
<p>
<strong>{$app.catname}</strong>
</p>
<foreach name="app.child" item='action'>
<dl>
<dt>
<strong>{$action.catnaem}</strong>
</dt>
        </dl>
          </foreach>
        </div>
      </foreach>