在两个数组值中的关联数组中查找最小值键是相同的[重复]

在两个数组值中的关联数组中查找最小值键是相同的[重复]

问题描述:

This question already has an answer here:

In PHP, say that you have an associative array like this:

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

How would I find the key with the lowest value and first matching? Here, I'd be looking for cats. Both cats and dogs are 1 value. But i need the first matching cause.

I am using

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

But it gives me the second key(dogs) always. I need to get first key(cats) for lowest value.Is it possible?

Thanks in advance.

</div>

check this-

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

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

check screenshot enter image description here

Check this :

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

     echo array_keys($pets)[0];

something like this :

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