PHP递归
最编程
2024-07-21 11:09:04
...
//模型公众方法,递归
public function treeLevel(array $data, int $pid = 0, string $html = '--', int $level = 0)
{
//静态数组不在方法内,所以无法覆盖
static $arr = [];
foreach ($data as $val) {
if ($val['pid'] == $pid) {
$val['html'] = str_repeat($html, $level * 2);
$val['level'] = $level + 1;
//如果是0,则是*菜单
$arr[] = $val;
$this->treeLevel($data, $val['id'], $html, $val['level']);
}
}
return $arr;
}
//递归处理层
public function sonLevel($data, $pid = 0)
{
$arr = [];
foreach ($data as $val){
if ($val['pid'] == $pid){
$val['son'] = $this->sonLevel($data,$val['id']);
$arr[] = $val;
}
}
return $arr;
}
上一篇: 用PHP实现三种遍历文件夹的递归方法示例
下一篇: 如何用PHP实现无限制层级的类别递归划分