我有二維數組,我必須在其中不斷比較下一個值。我正在使用 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>";
}
- 1 回答
- 0 關注
- 111 瀏覽
添加回答
舉報
0/150
提交
取消