Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am displaying the values of a multidimensional array. The values are being displayed but i am getting a notice of Undefined offset

Tags:

php

if($numrows>0)
{
    $i=0;
    while($i<count($result_page[$i]))         //This is line 68
    {
        echo "<tr>";
        echo "<td>".$result_page[$i]['product_id']."</td>";
        echo "<td>".$result_page[$i]['product_name']."</td>";
        echo "<td>".$result_page[$i]['product_price']."</td>";
        $i++;
    }
}

This is the notice:

Notice: Undefined offset: 10 in /home/jatin/web/www.exam.com/admin/productlist.php on line 68.

I am getting this notice because when the loop will be executed for the last time then $i will be incremented and it goes out of the length of the array.

Each time the number of elements in the 2nd dimension changes thus I have to use count function.

The notice occurs when the condition is checked for the last time, So all my elements are displayed but the notice occurs.

Please give an appropriate solution.

like image 855
Yash M. Hanj Avatar asked Apr 19 '18 10:04

Yash M. Hanj


3 Answers

well i found one solution myself also which is not a good option but it also works fine...........use the @ operator like given below ---

while($i<count(@$result_page[$i]))

Answers given by B.Desai and thavaamm are better options though.

like image 61
Yash M. Hanj Avatar answered Sep 28 '22 07:09

Yash M. Hanj


You can also use the following function to suppress the warnings.

error_reporting(E_ERROR | E_PARSE);
like image 41
Gamer Avatar answered Sep 28 '22 07:09

Gamer


Using the @ operator will suppress the notice. If only that is what you are looking for. Or you can also use this:

while(isset($result_page[$i]) && $i<count($result_page[$i])
like image 38
Just a Coder Avatar answered Sep 28 '22 08:09

Just a Coder