PHP:找出关联数组中具有相同值的所有键

Are there any functions in PHP that help find out all the keys with the same value in an associative array? For example, there is an array as follows:

$data1 = array("Peter" => "1", "Ann" => "1", "Susan" => "2", "Tom"=> "3");

I only want the keys with the value "1" (i.e. Peter and Ann). Thanks for help.

You can use array_keys with the optional second parameter:

array_keys() returns the keys, numeric and string, from the array.

If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

Use array_intersect and array_keys.

$data1 = array("Peter" => "1", "Ann" => "1", "Susan" => "2", "Tom"=> "3");
$find = 1;
Var_dump(array_keys(array_intersect($data1, [$find])));

https://3v4l.org/DICDB