Undefined offset原因以及解決辦法-php利用for遍歷二維數組方法-二維數組怎么遍歷


<?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遍歷的時候是不是利用了  <=(小於等於)。注意下這里是否錯誤即可解決!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM