<?php $arr3 = [ [1,2,3], [4,5,6,7], [8,9,10,11,12], ]; $len = count($arr3); $num2 = 0; $zong = 0; for($i = 0; $i < $len; $i++) { $tem = $arr3[$i]; $len2 = count($tem); for($j = 0; $j < $len2; $j++) { $num2 += $tem[$j]; $zong++; } } echo "<br>數組總和為: $num2"; echo "<br>數組個數為: $zong"; ?>
php利用for遍歷二維數組方法-教程-總結-問題
結果為:
數組總和為: 78
數組個數為: 12
問:
PHP 出現 Notice: Undefined offset:
functions - Undefined offset
php Notice: Undefined offset
Notice: Undefined offset 的解決方法
如果是遍歷數組的時候出現的這個報錯,觀察下for遍歷的時候是不是利用了 <=(小於等於)。注意下這里是否錯誤即可解決!