You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PHP 8中废弃的each循环转换为foreach循环?

正确改写PHP 8中失效的each循环为foreach循环

你的foreach写法语法顺序完全搞反了,正确的foreach遍历关联数组并同时获取键和值的格式是foreach(数组变量 as 键变量 => 值变量)。

另外原代码里的reset($value[1])可以直接去掉,因为foreach会自动从数组的起始位置开始遍历,不需要手动重置指针。

修改后的完整代码如下:

case pdf_parser::TYPE_DICTIONARY:

    // A dictionary.
    $this->_straightOut('<<');

    foreach ($value[1] as $k => $v) {
        $this->_straightOut($k . ' ');
        $this->_writeValue($v);
    }

    $this->_straightOut('>>');
    break;

内容的提问来源于stack exchange,提问作者Cindy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 07:06:21