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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1588353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:12:02  来源:igfitidea点击:

Finding the minimum value's key in an associative array

phpassociative-array

提问by Philip Morton

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.

我如何找到具有最低值的键?在这里,我会寻找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; catsor dogs.

以上,我不介意它是否只是输出;catsdogs

Thanks in advance.

提前致谢。

回答by SilentGhost

array_keysis 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 maxinstead of min, but I can distinctly remember it).

PS:SO 上的某个地方有一个 dup(它有max而不是min,但我可以清楚地记住它)。

回答by lexx

Thats how i did it.

我就是这样做的。

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

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

I hope that helps

我希望有帮助

回答by Jeff Ober

$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.

那么第一个键是最小值,它的值就是原始数组中的键。

回答by Phill Pafford

Might try looking into these:

可能会尝试研究这些:

回答by fyrye

Another approach for retrieving a single string is by using a desirable sorting methodand retrieving the first key directly by using key()on the sorted array. In this instance the key with the lowest value is desired, asortwill sort from lowest to highest values and reset the internal pointer. To retrieve the reverse (highest to lowest) use arsort.

检索单个字符串的另一种方法是使用理想的排序方法,并通过使用key()已排序数组直接检索第一个键。在这种情况下,需要具有最低值的键,asort将从最低值到最高值排序并重置内部指针。要检索反向(从最高到最低),请使用arsort.

Example: https://3v4l.org/5ijPh

示例:https: //3v4l.org/5ijPh

$pets = array(
   "dogs" => 2,
   "cats" => 1,
   "fish" => 3
);
asort($pets);
var_dump(key($pets));
//string(4) "cats"
$pets = array(
   "dogs" => 1,
   "cats" => 1,
   "fish" => 3
);
asort($pets);
var_dump(key($pets));
//string(4) "dogs"


Take note that all of the PHP array sorting methods will alter the array by-reference. To prevent altering the original array, create a copy of the array or use an Iterator.

请注意,所有 PHP 数组排序方法都会通过引用改变数组。为防止更改原始数组,请创建数组的副本或使用Iterator.

$petsSorted = $pets;
asort($petsSorted);
key($petsSorted);

回答by Rohit Suthar Mumbai

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