array repeating results

0

I'm not understanding why my array is repeating the results, my loop to go through it is like this:

for ($i = 0; $i < count($estados); $i++) {
   foreach ($estados[$i] as $key => $valor) {
      echo "<option value='".$estados[$i]['cod_estados']."'>".$estados[$i]['nome']."</option>";
   }
}

This loop is displayed in the select as follows:

ACRE
ACRE
ACRE
ACRE
ACRE
ACRE
ALAGOAS
ALAGOAS
ALAGOAS
ALAGOAS
ALAGOAS
ALAGOAS

etc ...

Here is an example of how the array is:

Array
(
[0] => Array
    (
        [cod_estados] => 1
        [0] => 1
        [sigla] => AC
        [1] => AC
        [nome] => ACRE
        [2] => ACRE
    )

[1] => Array
    (
        [cod_estados] => 2
        [0] => 2
        [sigla] => AL
        [1] => AL
        [nome] => ALAGOAS
        [2] => ALAGOAS
    )

[2] => Array
    (
        [cod_estados] => 3
        [0] => 3
        [sigla] => AP
        [1] => AP
        [nome] => AMAPÁ
        [2] => AMAPÁ
    )

Maybe it's a silly mistake, but I can not figure it out, I saw that it is repeating once for each item in the inner array.

    
asked by anonymous 15.09.2017 / 17:29

1 answer

2

You have for more in your code and foreach is specified $estados[$i] when only one foreach would solve. $valor is equal $estados[$i] so at the time of mounting the options just call it.

foreach ($estados as $valor) {
  echo "<option value='".$valor['cod_estados']."'>".$valor['nome']."</option>";
}
    
15.09.2017 / 17:37