JD Isaacks JD Isaacks -4 years ago 92
PHP Question

How to get array KEY from VALUE?

You can easily get an array value from a key like so:

$value = array[$key]
but what if I have the value and I want the key. What's the best way to get it?

Answer Source

You could use array_search() to find the first matching key.

From the manual:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download