亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號