Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse of array_search() that looks for the key instead of the value

PHP's array_search() does this:

Searches the array for a given value and returns the corresponding key if successful

I would like a function that does the exact opposite, that is, searches the array for a given key and returns the corresponding value if successful.

Is this available at all in PHP 5? If not, what solution would you suggest?

like image 341
pepe Avatar asked Nov 27 '11 00:11

pepe


People also ask

What is array_ reverse in PHP?

The array_reverse() function is used to reverse the order of the elements in an array.

Which function is used to get an array in the reverse order?

The array_reverse() function returns an array in the reverse order.

How do you check if a key exists in an array PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

How to print array backwards in PHP?

Answer: Use the PHP array_reverse() function You can use the PHP array_reverse() function to reverse the order of the array elements.


2 Answers

I am confused. Would $array[$key] not work?

like image 113
Dennis Avatar answered Sep 18 '22 01:09

Dennis


You can just use square bracket syntax, as follows:

$arr = array("key" => "value");
$v = $arr["key"]; // returns "value"
like image 27
Jon Newmuis Avatar answered Sep 19 '22 01:09

Jon Newmuis