-1

I have this $combo array :

    Array (
    [0] => 11.3
    [1] => 1.3
    [2] => 1.3
    [3] => 1.3
    )

then I try to process that array using foreach loop :

foreach ($combo as $value) {
   $key = array_keys($combo, $value);
}

the $key return unexpected on key 1, 2, 3 because I have duplicate value : 1.3

I want to have exact array key for each value. how to do this?

Saint Robson
  • 5,475
  • 18
  • 71
  • 118

1 Answers1

1

See foreach on PHP.net:

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

There are two syntaxes:

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

So you can do:

foreach ($combo as $key => $value) {
    echo $key; //0, 1, 2, 3 ...
    echo $value; //11.3, 1.3, 1.3, 1.3 ...
}
Ben
  • 8,894
  • 7
  • 44
  • 80