Find the key of an array by the given value?

4

I would like that through a given value I could return the array key. Example:

$array = array("primeiro" => 1, "segundo" => 2, "terceiro" => 3);

To find out if the value exists in the array there is the in_array(); function, but to find out the array key to which that value belongs? how can I do this in a simple way? using the given example, through the given value '1', find out the 'first' key.

    
asked by anonymous 11.08.2014 / 02:45

1 answer

6

Use the array_search () function, given a value if found it returns to corresponding array key.

demo

<?php
$arr = array("primeiro" => 1, "segundo" => 2, "terceiro" => 3);
$chave = array_search('1', $arr);

echo $chave;

Output:

primeiro
    
11.08.2014 / 03:00