PHP 从数组中获取最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6676768/
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-26 00:58:28  来源:igfitidea点击:

PHP Get Highest Value from Array

phparrayssorting

提问by Jamie McElwain

I'm trying to get hold of the largest value in an array, while still preserving the item labels. I know I can do this by running sort(), but if I do so I simply lose the labels - which makes it pointless for what I need. Here's the array:

我试图获取数组中的最大值,同时仍保留项目标签。我知道我可以通过运行 sort() 来做到这一点,但如果我这样做,我只会丢失标签 - 这使得我需要的东西毫无意义。这是数组:

array("a"=>1,"b"=>2,"c"=>4,"d"=>5);

Any ideas?

有任何想法吗?

回答by Karoly Horvath

Don't sort the array to get the largest value.

不要对数组进行排序以获得最大值。

Get the max value:

获取最大值:

$value = max($array);

Get the corresponding key:

获取对应的key:

$key = array_search($value, $array);

回答by borrible

If you just want the largest value in the array use the maxfunction. This will return the largest value, although not the corresponding key. It does not change the original array.

如果您只想要数组中的最大值,请使用max函数。这将返回最大值,尽管不是相应的键。它不会更改原始数组。

If you care about the the key you could then do

如果你关心你可以做的钥匙

$key = array_search(max($array), $array)

(Edited to include @binaryLV's suggestion)

(编辑以包含@binaryLV 的建议)

回答by Rakesh

$a = array(10, 20, 52, 105, 56, 89, 96);
$b = 0;
foreach ($a as $key=>$val) {
    if ($val > $b) {
        $b = $val;
    }
}
echo $b;

回答by Jacob

You are looking for asort()

您正在寻找asort()

回答by binaryLV

You could use max()for getting the largest value, but it will return just a value without an according index of array. Then, you could use array_search()to find the according key.

您可以使用max()来获取最大值,但它只会返回一个没有相应数组索引的值。然后,您可以使用array_search()查找相应的键。

$array = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
$maxValue = max($array);
$maxIndex = array_search(max($array), $array);
var_dump($maxValue, $maxIndex);

Output:

输出:

int 5
string 'd' (length=1)

If there are multiple elements with the same value, you'll have to loop through array to get all the keys.

如果有多个具有相同值的元素,则必须遍历数组以获取所有键。

It's difficult to suggest something good without knowing the problem. Why do you need it? What is the input, what is the desired output?

在不了解问题的情况下很难提出好的建议。你为什么需要它?什么是输入,什么是期望的输出?

回答by ajay Sharavat

$abc=array("a"=>1,"b"=>2,"c"=>4,"e"=>7,"d"=>5);
/*program to find max value*/
$lagest = array();
$i=0;
foreach($abc as $key=>$a) {
    if($i==0) $b=$a;
    if($b<$a) {
        $b=$a;
        $k=$key;
    }
    $i++;
 }
 $lagest[$k]=$b;
 print_r($lagest);

回答by Hanifeoglu

<?php 
$array = array("a"=>1,"b"=>2,"c"=>4,"d"=>5); 

foreach ($array as $key => $value) {
   if ($value >= $max) 
    $max = $key;     
}
echo " The array in largest number :".$max."<br/>";
?> 

回答by Vladymyr Drogovoz

You need to use by ksort(array("a"=>1,"b"=>2,"c"=>4,"d"=>5)); for more info: http://www.w3schools.com/php/php_arrays_sort.asp

您需要使用 by ksort(array("a"=>1,"b"=>2,"c"=>4,"d"=>5)); 更多信息:http: //www.w3schools.com/php/php_arrays_sort.asp

回答by Ashish pathak

greatestValue=> try this its very easy

bestValue=> 试试这个很简单

$a=array(10,20,52,105,56,89,96);
$c=0;
foreach($a as $b)
{
if($b>$c)
$c=$b;

}
echo $c;

回答by erenon

// assuming positive numbers

$highest_key;
$highest_value = 0;
foreach ($array as $key => $value) {
    if ($value > $highest_value) {
        $highest_key = $key;
    }
}

// $highest_key holds the highest value