为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 将值与数组中的 for 循环进行比较会给出偏移量错误

PHP 将值与数组中的 for 循环进行比较会给出偏移量错误

PHP
呼啦一阵风 2023-04-21 14:13:21
我有二维数组,我必须在其中不断比较下一个值。我正在使用 for 循环遍历所有值,但出现偏移错误,因为当它到达最后一个数组时,它想检查下一个不存在的数组。我怎样才能防止这种情况发生?我知道 ($items[$row][0]!=$items[$row+1][0]) 是问题所在。我不应该使用 for 循环吗?我不明白的是下面的代码没有给出任何错误。如果 $items[$row+1][0] 是到达最后一个数组时的问题,那么 $items[$row-1][0] 不应该在检查第一个数组时也给出错误阵列?if ($row==0 || ($row>0 && $items[$row][0]!=$items[$row-1][0]) )但是这个不行。注意:尝试访问 null 类型值的数组偏移量注意:未定义的偏移量        if (($row<$num_rows && ($items[$row][0]!=$items[$row+1][0]))||$num_rows==$is_odd)  {        //$is_odd is the number of last array.          //$num_rows is the total number of arrays.         echo "</table></div></div>";    }
查看完整描述

1 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

在你的情况下检查下一个索引是否存在


if ((!empty($items[$row + 1]) && $row < $num_rows && ($items[$row][0] != $items[$row + 1][0])) || $num_rows == $is_odd) {

    //$is_odd is the number of last array.  

    //$num_rows is the total number of arrays. 

    echo "</table></div></div>";

}


查看完整回答
反对 回复 2023-04-21
  • 1 回答
  • 0 关注
  • 64 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信