Search This Blog

Friday, September 23, 2011

HowTo: Clear PHP undefined offset notice when looping through arrays

undefined offset notice when looping through arrays in php - Stack Overflow

PHP Notice:  Undefined offset: 118 in /usr/share/resultsdb/www/index.php on line 465, referer: ...

While this seems to simple to write about, there weren't any posts with the answer. Even one specifically about sparse matrices didn't answer the question.
There is also bug in PHP is_array() which started this journey in the first place.
https://bugs.php.net/bug.php?id=55772

There are several HowTo's on 'PHP Notice Undefined offset' from webmasters wondering what is filling their logs, to lectures on bad programming.No one answers the question.

I have a sparse matrix, that needs each element to display on an html page. So when the matrix is loaded with defined values there are many undefined values, 6158 for example.

This is displayed as a square matrix using an HTML table - A conflict between good PHP programming and HTML programming. The undefined elements need some html, so the 'for loops' must touch each element of the array.

is_array is the correct call to use. It tests array elements, and returns true or false if they are valid or not.

While the is_array function returns true and false - PHP still issues the warning message.

For now, the isset function can be used. It's not obvious that is applies to arrays, but it works.

$data_array=array();
$data_array[2][10] = "defined";
$row_count = 10;
$col_count = 10;

//...fill with the defined values...many values undefined.

for ($r = 0; $r < $row_count; $r++)
{
echo "";
  for ($c = 0; $c < $col_count; $c++)
  {
     if (isset($data_array[$r+1][$c+1])){
       $data = $data_array[$r+1][$c+1];
     } else {
       $data = NULL;
     }
    echo "";
  }
}

1 comment:

  1. array(0=>"BREAD",1=>"MILK",2=>"COFFEE",3=>"JUICE",4=>"COOKIE",5=>"JAM"),1=>array(0=>"BREAD",1=>"MILK",2=>"JAM",3=>"COFFEE",4=>"MANGO",5=>"APPLE"),2=>array(0=>"BREAD",1=>"MILK",2=>"COFFEE",3=>"JAM",4=>"BREAD",5=>"MILK"),3=>array(0=>"BREAD",1=>"MILK",2=>"JUICE",3=>"JUICE",4=>"MILK",5=>"JAM"),4=>array(0=>"BREAD",1=>"MILK",2=>"COOKIE",3=>"MANGO",4=>"APPLE",5=>"JAM"));
    $itemlist = array("BREAD", "MILK", "COFFEE","JUICE","COOKIE","JAM");
    for ($i = 0; $i <5; $i++)
    {
    for ($j = 0; $j <6; $j++)
    {
    $item[$i][$j]=0;
    }}
    for($i=0;$i<5;$i++)
    {
    for($j=0;$j<6;$j++)
    {
    if(strpos($transaction[$i][$j],$itemlist[$j]==true))
    {$item[$i][$j]=1;
    }
    else
    {
    $item[$i][$j]=0;
    }
    }
    }
    for($j=0;$j<6;$j++)
    { for($i=0;$i<5;$i++)
    {if($item[$i][$j]==1)
    { $nt[$j]=$nt[$j]+1;
    }}
    echo "NUMBER OF ITEM ".$itemlist[$j]." IS HERE" .$nt[$j];

    }
    for($j=0;$j<6;$j++)
    { if(($nt[$j]*20)>=50)
    $q[$j]=1;
    else
    $q[$j]=0;

    if($q[$j]==1)
    {$t1++;
    echo "Item ".$itemlist[j]." is select";

    }
    }
    for($j=0;$j<6;$j++)
    { for($i=0;$i<5;$i++)
    {

    if($q[$j]==0)
    {
    $item[$i][$j]=0;
    }
    }
    }
    for($j=0;$j<6;$j++)
    { for($m=$j+1;$m<6;$m++)
    { for($i=0;$i<5;$i++)
    { if($item[$i][$j]==1 && $item[$i][$m]==1)
    { $nt1[$j][$m]=$nt1[$j][$m]+1;
    }
    }
    if($nt1[$j][$m]!=0)
    echo "Number of Items of "+$itemlist[$j]+"& "+$itemlist[$m]+" :"+$nt1[$j][$m];
    }

    }

    for($j=0;$j<6;$j++)
    { for($m=$j+1;$m<6;$m++)
    {
    if(($nt1[$j][$m]*20)>=50)
    $q[$j]=1;
    else
    $q[$j]=0;

    if($q[$j]==1)
    {
    echo "Item ".$itemlist[$j]."& ".$itemlist[$m]." is selected ";

    }
    }

    }



    ?>
    I am a getting a error undefined offset
    Please help

    ReplyDelete