The inverse of array_search (), which searches for a key instead of a value

PHP array_search() does this:

Distorts the array for the given value and returns a successful key

I need a function that does the exact opposite, i.e. looks for an array for the given key and returns a successful value .

Is this even possible in PHP 5? If not, what solution would you suggest?

+7
source share
3 answers

You can simply use the square bracket syntax as shown below:

 $arr = array("key" => "value"); $v = $arr["key"]; // returns "value" 
+10
source

I'm confused. Does $array[$key] not work?

+11
source

To keep up with the PHP tradition

 function array_search_reverse($needle, $haystack){ return $haystack[$needle]; } 
+2
source

All Articles