在两个数组值相同的关联数组中查找最小值的键

Finding the minimum value's key in an associative array in two arrray values are same

在 PHP 中,假设您有一个这样的关联数组:

$pets = array(
   "cats" => 1,
   "dogs" => 1,
   "fish" => 2
);

如何找到具有最低值和第一个匹配项的键?在这里,我会寻找猫。 cats 和 dogs 都是 1 值。但是我需要第一个匹配的原因。

我正在使用

array_keys($pets, min($pets));

但它总是给我第二把钥匙(狗)。我需要获得最低 value.Is 的第一把钥匙(猫)吗?

提前致谢。

检查这个-

$pets = array(
    "cats" => 1,
    "dogs" => 1,
    "fish" => 2
);

echo array_search(min($pets), $pets);

查看截图

检查这个:

  $pets = array(
       "cats" => 1,
       "dogs" => 1,
       "fish" => 4
    );
    asort($pets);

     echo array_keys($pets)[0];

像这样:

$pets = array(
    "cats" => 1,
    "dogs" => 1,
    "fish" => 2
);
echo min(array_keys($pets));