本文探讨foreach循环中使用回调函数时可能出现的累积结果问题,并提供相应的解决方案。
在使用foreach循环迭代数据并调用回调函数getchildareaid获取子区域ID时,发现结果存在累积现象。代码如下:
foreach ($towns as $key => $val) { $areaidarr = getchildareaid($val['id']); $result[$val['name']] = $worker::where('area_id', 'in', $areaidarr)->count(); }
回调函数getchildareaid定义如下:
function getchildareaid($id) { static $area; $area = $area ?? new \app\common\model\area; $result = collection($area->where(['pid' => $id])->order('id desc')->select())->toArray(); static $res = []; if ($result) { foreach ($result as $key => $val) { $res[] = $val['id']; getchildareaid($val['id']); } } return $res; }
问题在于,getchildareaid函数由于使用了静态变量$res,导致每次调用时都累积了之前的结果,而非每次独立计算。
getchildareaid函数中static $res = [];声明了一个静态变量。静态变量在函数调用结束后不会被销毁,其值会保留在后续调用中。因此,每次foreach循环调用getchildareaid时,$res都会累积新的结果,最终导致结果错误。
为了解决这个问题,避免使用静态变量,可以采用以下两种方法:
重写函数,使用局部变量: 创建一个新的函数,不使用静态变量$res,而是使用局部变量来存储每次调用的结果。
修改现有函数,消除静态变量: 修改getchildareaid函数,移除静态变量$res,并使用递归调用时返回新的数组,避免结果累积。 修改后的函数如下:
function getChildAreaId($id) { $area = new \app\common\model\Area; $result = collection($area->where(['pid' => $id])->order('id desc')->select())->toArray(); $res = []; if ($result) { foreach ($result as $key => $val) { $res[] = $val['id']; $res = array_merge($res, getChildAreaId($val['id'])); } } return $res; }
此修改后的函数在每次递归调用时,都会创建一个新的$res数组,并将递归结果合并到当前的$res中,从而避免了结果累积的问题。 选择哪种方案取决于代码的整体结构和可维护性。 通常情况下,重写函数更清晰易懂。
通过以上方法,可以有效解决foreach循环中回调函数结果累积的问题,确保每次调用回调函数都能得到正确的结果。
以上就是在 Foreach 循环中使用回调函数时,为什么会出现结果累积的问题?如何解决?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号