How do i get the key of an array element if i only have the value?
How do i get the key of an array element if i only have the value?
So i have this array and i need the key of an email address with the value
$users_emails = array(
'Spence' => 'spence@someplace.com',
'Matt' => 'matt@someplace.com',
'Marc' => 'marc@someplace.com',
'Adam' => 'adam@someplace.com',
'Paul' => 'paul@someplace.com');
How do i get the 'Adam' if all i have is the value 'adam@someplace.com'.. so basically how do 开发者_运维技巧i get the key if i have the value
Use array_search and this question.
Example from docs:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
PHP has wonderful documentation and it's quite easy to find the basic stuff with Google.
<?php
$key = array_search('matt@someplace.com', $users_emails); // $key = 'Spence';
?>
$email = 'adam@someplace.com';
$name = array_search($email, $users_emails);
var_dump($name === 'Adam');
See array_search()
Use array_search
$key = array_search('adam@someplace.com', $users_emails);
精彩评论