Search code examples
phpexceptionundefinedoffset

Note: Undefined Offset: 2


The answers I found for this exception were not very helpful. Can someone please tell me what is wrong here?

Thanks

    for ($x = 0; $x <= $cartcount; $x++)
    {
      $innerarray = $cart[$x];              //this is the exception-producing line
      $quantity = $innerarray['quantity'];
      $title = $innerarray['articleTitle'];
      $articlenumber = $innerarray['articleNumber'];

      $outputQNT[] = $innerarray['quantity'];

      $outputTITLE[] = $innerarray['articleTitle'];

      $outputATNBR[] = $innerarray['articleNumber'];
    }

Solution

  • Array start form zero & end with total array length-1. so in for loop:

    for ($x = 0; $x <$cartcount; $x++){}
    

    if $cartcount = 2 then array index are [0,1]

    If still there is problem then use :

    array_values($cartcount)
    

    It will return array with proper indexing