案例,在做递归无限级子类的时候,在foreach循环里,静态变量会每一次循环都累加起来,如果需要每一次循环是一类的话,就需要每次循环都销毁一次静态变量(推荐学习:PHP编程从入门到精通)
public function getTree($data, $pid = 0, $level = 0, $bool_level = true ,$clear=false ) {
if ($pid < 1) {
return false;
}
static $arr = [];
if($clear){
$arr = null;
static $arr = [];
}
if ($bool_level) {
$level = $level - 1;
} else {
$level = 1;
}
if ($level >= 0) {
foreach ($data as $key => $val) {
if ($val['pid'] == $pid) {
$arr[$level + 1][] = $val['id'];
$this->getTree($data, $val['id'], $level, $bool_level);
}
}
}
return $arr;
}