PHP 从数组中获取最高值

发布于 2024-11-19 16:45:11 字数 184 浏览 0 评论 0原文

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

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

有什么想法吗?

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:

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

夏有森光若流苏 2024-11-26 16:45:12

您可以使用 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)

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

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

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.

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

风启觞 2024-11-26 16:45:12

最大价值=>试试这个,很简单

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

}
echo $c;

greatestValue=> try this its very easy

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

}
echo $c;
○愚か者の日 2024-11-26 16:45:12
$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";
$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";
你对谁都笑 2024-11-26 16:45:12

尝试一下。

<代码>
$data = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
$maxKey = 当前(array_keys($data, max($data)));
var_dump($maxKey);

Try it.


$data = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
$maxKey = current(array_keys($data, max($data)));
var_dump($maxKey);

爱你是孤单的心事 2024-11-26 16:45:12
// 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
// 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
零度℉ 2024-11-26 16:45:12
$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);
$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);
跨年 2024-11-26 16:45:12
<?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/>";
?> 
<?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/>";
?> 
鲜肉鲜肉永远不皱 2024-11-26 16:45:12

找到最大的数,包括负数:

return max([abs(max($array)),abs(min($array))]);

Find highest number, including negative:

return max([abs(max($array)),abs(min($array))]);
摇划花蜜的午后 2024-11-26 16:45:12

asort() 是要走的路:

$array = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
asort($array);
$highestValue       = end($array);
$keyForHighestValue = key($array);

asort() is the way to go:

$array = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
asort($array);
$highestValue       = end($array);
$keyForHighestValue = key($array);
生寂 2024-11-26 16:45:12

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

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

不乱于心 2024-11-26 16:45:12

这是练习中的解决方案:

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");

Here a solution inside an exercise:

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");
爱你不解释 2024-11-26 16:45:12

不使用 PHP 内置函数查找最大数

<?php
    $array = array(1,2,3);
    $flag = 0;
    foreach($array as $key=>$val){
        if($val > $flag){
           $flag = $val;
        }
        echo "Max value with each iteration: ".$flag."<br/>";
    }
    echo "Final max value: ".$flag;
    ?>

Find largest number without using built-in functions in PHP

<?php
    $array = array(1,2,3);
    $flag = 0;
    foreach($array as $key=>$val){
        if($val > $flag){
           $flag = $val;
        }
        echo "Max value with each iteration: ".$flag."<br/>";
    }
    echo "Final max value: ".$flag;
    ?>
尽揽少女心 2024-11-26 16:45:12

尝试使用 asort()

来自文档:

asort - 对数组进行排序并维护索引关联

描述:

bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

此函数对数组进行排序,以便数组索引保持其原来的值
与它们关联的数组元素的相关性。这是
主要在对关联数组进行排序时使用,其中实际元素
顺序很重要。

Try using asort().

From documentation:

asort - Sort an array and maintain index association

Description:

bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

This function sorts an array such that array indices maintain their
correlation with the array elements they are associated with. This is
used mainly when sorting associative arrays where the actual element
order is significant.

撩发小公举 2024-11-26 16:45:11

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

获取最大值:

$value = max($array);

获取对应的key:

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

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

Get the max value:

$value = max($array);

Get the corresponding key:

$key = array_search($value, $array);
寂寞陪衬 2024-11-26 16:45:11

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

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

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

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

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

If you care about the the key you could then do

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

(Edited to include @binaryLV's suggestion)

2024-11-26 16:45:11
$a = array(10, 20, 52, 105, 56, 89, 96);
$b = 0;
foreach ($a as $key=>$val) {
    if ($val > $b) {
        $b = $val;
    }
}
echo $b;
$a = array(10, 20, 52, 105, 56, 89, 96);
$b = 0;
foreach ($a as $key=>$val) {
    if ($val > $b) {
        $b = $val;
    }
}
echo $b;
¢好甜 2024-11-26 16:45:11

您正在寻找 asort()

You are looking for asort()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文