-1

I have to get index element of array by value.

Suppose i have bellow snippet array.

Array ( [2671] => 24 [3149] => 1 [3711] => 2 [3695] => 16
 [3209] => 53 [3638] => 16 [3671] => 22 [3235] => 19 
[3773] => 10 [348] => 1 [3387] => 2 [3787] => 1 [3693] => 1
 [3248] => 28 [3816] => 2 [3060] => 3 [3200] => 2 [3741] => 2 
[3676] => 26 [3855] => 3 [3196] => 4 [3030] => 1 ) 

And i am getting highest 3 values from this array as bellow output.

Top value 53
Top value 28
Top value 26

So now i want get the element for value 53 is [3209] and for other values also like

28 is [3248]
26 is [3676]

How do get this.

0

3 Answers 3

7

array_search() would help. array_search() returns the index for the searched value.

echo '53 is [' . array_search(53, $array) . ']';
Sign up to request clarification or add additional context in comments.

Comments

1

Try :

$key = array_search('28', $array); // it will return the value

Comments

0

use array_flip() as follows

$input = array(2671=>24, 3248=>28, 3676=>26);
$flipped = array_flip($input);
$result = $flipped[28];

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.