PHP从数组中获得最高价值
我试图获取数组中的最大值,同时仍保留项目标签。 我知道我可以通过运行sort()来做到这一点,但是如果这样做,我只会丢失标签-这对于我需要的东西毫无意义。 这是数组:
array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
有任何想法吗?
15个解决方案
131 votes
不要对数组进行排序以获取最大值。
获取最大值:
$value = max($array);
获取相应的密钥:
$key = array_search($value, $array);
Karoly Horvath answered 2020-06-22T20:10:28Z
17 votes
如果只希望数组中的最大值,请使用max函数。 这将返回最大值,尽管不是相应的键。 它不会更改原始数组。
如果您关心钥匙,那么可以做
$key = array_search(max($array), $array)
(编辑后包含@binaryLV的建议)
borrible answered 2020-06-22T20:10:56Z
4 votes
您正在寻找asort()
Jacob answered 2020-06-22T20:11:16Z
4 votes
您可以使用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);
输出:
int 5
string 'd' (length=1)
如果有多个具有相同值的元素,则必须遍历数组以获取所有键。
不知道问题就很难提出好的建议。 你为什么需要它? 输入是什么,期望的输出是什么?
binaryLV answered 2020-06-22T20:11:50Z
4 votes
$a = array(10, 20, 52, 105, 56, 89, 96);
$b = 0;
foreach ($a as $key=>$val) {
if ($val > $b) {
$b = $val;
}
}
echo $b;
Rakesh answered 2020-06-22T20:12:05Z
0 votes
// 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
erenon answered 2020-06-22T20:12:21Z
0 votes
$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
$b=$a;
$k=$key;
}
$i++;
}
$lagest[$k]=$b;
print_r($lagest);
ajay Sharavat answered 2020-06-22T20:12:36Z
0 votes
$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."
";
?>
Hanifeoglu answered 2020-06-22T20:12:51Z
0 votes
您需要使用ksort(array(“ a” => 1,“ b” => 2,“ c” => 4,“ d” => 5));有关更多信息:[http://www.w3schools.com/php/php_arrays_sort.asp]
Vladymyr Drogovoz answered 2020-06-22T20:13:11Z
0 votes
greatValue =>试试这个很简单
$a=array(10,20,52,105,56,89,96);
$c=0;
foreach($a as $b)
{
if($b>$c)
$c=$b;
}
echo $c;
Ashish pathak answered 2020-06-22T20:13:31Z
0 votes
查找最高数字,包括负数:
return max([abs(max($array)),abs(min($array))]);
Jason Mullings answered 2020-06-22T20:13:51Z
0 votes
$ee = array('a' => 50, 'b' => 25, 'c' => 5, 'd' => 80, 'e' => 40, 'f' => 152, 'g' => 45, 'h' => 28);
$Acurr = '';
$Amax = 0;
foreach($ee as $key => $value) {
$Acurr = $value;
if($Acurr >= $Amax) {
$Amax = $Acurr;
}
}
echo "greatest number is $Amax";
Khushal Singh Dangwal answered 2020-06-22T20:14:07Z
0 votes
这是练习中的解决方案:
function high($sentence)
{
$alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'ñ', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
$alphabet = array_flip($alphabet);
$words = explode(" ", $sentence);
foreach ($words as $word) {
$letters = str_split($word);
$points = 0;
foreach ($letters as $letter)
$points += $alphabet[$letter];
$score[$word] = $points;
}
$value = max($score);
$key = array_search($value, $score);
return $key;
}
echo high("what time are we climbing up the volcano");
Erich García answered 2020-06-22T20:14:26Z
-1 votes
asort()是必经之路:
$array = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
asort($array);
$highestValue = end($array);
$keyForHighestValue = key($array);
Stefan Gehrig answered 2020-06-22T20:14:46Z
-2 votes
尝试使用bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )。
从文档:
asort-对数组进行排序并维护索引关联
描述:
bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
此函数对数组进行排序,以使数组索引保持其 与它们关联的数组元素的相关性。 这是 主要用于排序实际元素所在的关联数组时 订单意义重大。
SergeS answered 2020-06-22T20:15:28Z