Error with Foreach arguments

Asked

Viewed 366 times

1

I created a structure with some loops however php is returning me that the foreach arguments are invalid... I’m not sure if this structure is correct, so I’d like someone to let me know if this is correct or not:

for($x=1; x<=$n_cartelas_registradas; $x++) {
$contador[$x] = 0;
foreach($cartela[$x] as $n_cartela=>$numero){ //linha retornando erro
  for($a=0; $a<=$n_sort; $a++){
    if($numero == $sorteados_array[$a]){
     $contador[$x]++; 
       }
    }
  }
}

All variables used have values. The cartela array has the following structure: $cartela[1] = array (n, q, e, r, t); and these values are not arrays. That is, it is two-dimensional. Already the $sorteados_array is as follows: $sorteados_array[0] = x; i.e.; it is a one-dimensional array.

So the foreach arguments can be written that way? If yes, where can the error be? If not, how can I get the expected result correctly? Thank you.

  • 2

    N, q, e, r, t are not arrays but is two-dimensional? It’s not at all clear.. Please post a $cartela dump

  • By your code I estimate that you are forgetting to initialize any of the arrays inside the $cartela; variable.?

  • We’d really need to see what’s on $cartela. But I broke your loop of 1 until $n_cartelas_registradas. Shouldn’t be of 0 to $n_cartelas_registradas - 1?

  • I was wrong to write... They are arrays.

1 answer

2


Missing dollar sign in variable $x of condition.

for($x=1; x <= $n_cartelas_registradas; $x++)
----------^

Exchange for:

for($x=1; $x <= $n_cartelas_registradas; $x++) {

Browser other questions tagged

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