-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?

2

1 Answer 1

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 ... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.