在关联数组中查找最小值的键

时间:2022-08-22 13:04:07

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

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

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

How would I find the key with the lowest value? Here, I'd be looking for cats.

我怎样才能找到价值最低的钥匙?在这里,我会寻找猫。

Is there some built in PHP function that I've missed which does this? It would also be great if there was a solution that accounted for several values being identical, as below:

是否有一些内置的PHP功能,我错过了这个吗?如果有一个解决方案可以解决几个相同的值,如下所示:

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

Above, I wouldn't mind if it just output either; cats or dogs.

在上面,我不介意它是否只输出;猫或狗。

Thanks in advance.

提前致谢。

5 个解决方案

#1


85  

array_keys is your friend:

array_keys是你的朋友:

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

P.S.: there is a dup here somewhere on SO (it had max instead of min, but I can distinctly remember it).

P.S。:SO上有一个副本(它有max而不是min,但我可以清楚地记住它)。

#2


8  

Thats how i did it.

多数民众赞成我是怎么做到的。

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

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

I hope that helps

我希望有所帮助

#3


2  

Might try looking into these:

可能会尝试调查这些:

#4


2  

$min_val = null;
$min_key = null;
foreach($pets as $pet => $val) {
  if ($val < $min_val) {
    $min_val = $min;
    $min_key = $key;
  }
}

You can also flip the array and sort it by key:

您还可以翻转数组并按键对其进行排序:

$flipped = array_flip($pets);
ksort($flipped);

Then the first key is the minimum, and its value is the key in the original array.

然后第一个键是最小值,它的值是原始数组中的键。

#5


-2  

find the highest value

找到最高价值

print max(120, 7, 8, 50);

returns --> 120

返回 - > 120

$array = array(100, 7, 8, 50, 155, 78);
print max($array);

returns --> 155

返回 - > 155

find the lowest value

找到最低价值

print min(120, 7, 8, 50);

returns --> 7

返回 - > 7

$array = array(50, 7, 8, 101, 5, 78);
print min($array);

returns --> 5

返回 - > 5

#1


85  

array_keys is your friend:

array_keys是你的朋友:

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

P.S.: there is a dup here somewhere on SO (it had max instead of min, but I can distinctly remember it).

P.S。:SO上有一个副本(它有max而不是min,但我可以清楚地记住它)。

#2


8  

Thats how i did it.

多数民众赞成我是怎么做到的。

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

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

I hope that helps

我希望有所帮助

#3


2  

Might try looking into these:

可能会尝试调查这些:

#4


2  

$min_val = null;
$min_key = null;
foreach($pets as $pet => $val) {
  if ($val < $min_val) {
    $min_val = $min;
    $min_key = $key;
  }
}

You can also flip the array and sort it by key:

您还可以翻转数组并按键对其进行排序:

$flipped = array_flip($pets);
ksort($flipped);

Then the first key is the minimum, and its value is the key in the original array.

然后第一个键是最小值,它的值是原始数组中的键。

#5


-2  

find the highest value

找到最高价值

print max(120, 7, 8, 50);

returns --> 120

返回 - > 120

$array = array(100, 7, 8, 50, 155, 78);
print max($array);

returns --> 155

返回 - > 155

find the lowest value

找到最低价值

print min(120, 7, 8, 50);

returns --> 7

返回 - > 7

$array = array(50, 7, 8, 101, 5, 78);
print min($array);

returns --> 5

返回 - > 5