Invalid argument provided for foreach

Asked

Viewed 41 times

-2

I created a foreach to traverse a array which has 2 element but I wanted my foreach to get only the first element, I tried to use the [0] but my return was that mistake:

ERROR:

arning: Invalid argument supplied for foreach() in C:\Apache2\htdocs\teste.php on line 44

My code:

foreach($lista_quantidade[0] as $quantidade_unidade){
       $quantidade_unidade = intval($quantidade_unidade);
       $quantidade_unida_diferen = $quantidade - $quantidade_unidade;
       echo "Diferença de quantidade: $quantidade_unida_diferen" . "<br>";
      }

1 answer

0


If you want to take the [0] element of your array, why do you need to go through it? You don’t need a foreach to have that value.

  • 1

    You were right not to call me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.