如何返回具有最高值的数组元素?

时间:2015-11-10 07:32:11

标签: php

在我的代码中,我有两个具有相同年龄"Joe"=>"43""Rob"=>"43"的元素。

我的代码输出是:

  

我想输出:

  

Joe和Rob

因为它们具有最高价值。

这是我的代码:

<?php
    $cur = 1;

    $age = array("Peter" => "35", "Ben" => "37", "Joe" => "43", "Rob" => "43");
    $new_array = array();
    arsort($age);
    $new_array = $age;
    $key = array_search(max($new_array), $new_array);

    while ($cur > 0) {
        echo $key;
        $cur--;
    }
?>

7 个答案:

答案 0 :(得分:12)

我更改了数组中的键和值,然后按键排序并返回第一个键的值:

$ages = array("Peter" => "35", "Ben" => "37", "Joe" => "43", "Rob" => "43");
$new = array();

foreach ($ages as $name => $age) {
  $new[$age][] = $name;
}

uksort($new, function($ka, $kb) { return $kb - $ka; }); // or just krsort($new);
$new = array_values($new)[0]; // <- to use this you have to have at least PHP 5.4

// if you have PHP < 5.4 you can simply do it in two steps:
// $new = array_values($new);
// $new = $new[0];

See it in action!

编辑:even simpler!

$ages = array("Peter" => "35", "Ben" => "37", "Joe" => "43", "Rob" => "43");
$max = max($ages);
$new = array_keys(array_filter($ages, function ($age) use ($max) { return $age == $max; }));

答案 1 :(得分:10)

使用:

$people = array("Peter" => "35", "Ben" => "37", "Joe" => "43", "Rob" => "43");
$max = max($people);
$result = array_filter($people, function($age) use ($max){ return $max == $age; });

结果是:

Array
(
    [Joe] => 43
    [Rob] => 43
)

答案 2 :(得分:4)

只需手动检查:

$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43", "Rob" => "43");
$new_array = array();
arsort($age);
$new_array = $age;
$max = max($new_array);

$results = array();
foreach ($new_array as $key => $val) {
    if ($val == $max) {
        $results[] = $key;
    }
}

echo implode(' and ', $results);
// will output "joe and bob"

答案 3 :(得分:4)

我更喜欢@ matteo-tassinari和@evilive的答案,并希望自己提出建议。但是,由于效率问题出现了,这里只有一个循环,因此具有线性时间复杂度:

<?php
$max = ~PHP_INT_MAX;
$result = [];
foreach($age as $key => $value) {
    if($value > $max) {
        $result = [ $key => $value ];
        $max = $value;
    }
    if($value == $max) {
        $result[$key] = $value;
    }
}
var_dump($result);

还有另一个使用bucket sort的解决方案:

function bucket($ages) {
    $buckets = [];
    foreach($ages as $key => $value) {
      $buckets[$value][] = $key;
    }
    return $buckets[max(array_keys($buckets))];
}

关于性能和可伸缩性的讨论,我为四种提议的解决方案(循环,sortfilter,存储桶)编写了一个小的基准脚本:

<?php
function loop($ages) {
    $max = 0;
    $result = [];
    foreach($ages as $key => $value) {
            if($value > $max) {
                    $result = [ $key => $value ];
                    $max = $value;
            }
            if($value == $max) {
                    $result[$key] = $value;
            }
    }
    return array_keys($result);
}

function filter($ages) {
    $max = max($ages);
    $new = array_filter($ages, function ($age) use ($max) { return $age == $max; });
    return array_keys($new);
}

function bucket($ages) {
    $buckets = [];
    foreach($ages as $key => $value) {
            $buckets[$value][] = $key;
    }
    return $buckets[max(array_keys($buckets))];
}


for($n = 2; $n < 10000000; $n*=2) {

    $ages = [];
    for($i = 0; $i < $n; $i++) {
            $ages['name_'.$i] = rand(0,100);
    }

    $start = microtime(true);
    echo $n.';';
    loop($ages);
    echo (microtime(true) - $start).';';
    $start = microtime(true);
    arsort($ages);
    echo (microtime(true) - $start).';';
    $start = microtime(true);
    filter($ages);
    echo (microtime(true) - $start).';';
    bucket($ages);
    echo (microtime(true) - $start).';';
    echo PHP_EOL;
}

Limited Live Test

请仔细检查这是否正确:在命令行中使用php-5.6.15,我的时间看起来像这样:

elements;loop;sort;filter;bucket
...
4096;0.001507;0.009868;0.01211;0.01453;
8192;0.003704;0.002483;0.02488;0.03035;
16384;0.006660;0.01010;0.05396;0.06723;
32768;0.01417;0.01271;0.09163;0.1163;
...
1048576;0.4227;0.9791;2.983;3.943;
2097152;0.8572;2.320;6.064;8.020;
4194304;1.787;4.981;11.90;16.04;
8388608;3.892;10.84;24.76;33.31;

对于少量元素,方法之间的差异并不大,但正如您所看到的,对于最大值,loop方法比sort快两倍,快8倍比filterbucket,比loop快十一倍。因此,如果您的数组是 huge ,则应使用app.get('/user',function(req, res) { User.find({region: "NA",sector:"Some Sector"}, function(err, user) { if (err) { res.send(err); } console.log(user); res.json(user); }); });

答案 4 :(得分:3)

您可以使用数组方法 https://stackoverflow.com/a/6153713/217022 next

使用 next(),您将数组指针移动一个位置。使用键(),您将获得数组指针元素的键。所以最终的代码将是这样的:

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Rob"=>"43");
arsort($age);
echo key($age);

next($age);
echo key($age);

key

答案 5 :(得分:3)

我会做这样的事情

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Rob"=>"43");

$max = max($age); //get the highest age

foreach ($age as $key => $value) { //iterate through $age array
    if ($value == $max) {          //if the value is equal to max age 
        echo $key."<br />";        // then echo the key
    }
}

答案 6 :(得分:1)

我想知道为什么没人使用内置解决方案:

$age = array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Rob"=>"43");
$new = array_keys($input, max($age));

返回 array('Joe','Rob')

https://www.php.net/manual/en/function.array-keys.php说:

  

array_keys(array $ array,混合$ search_value [,bool $ strict = FALSE]):array

     

如果指定了search_value,则仅返回该值的键。否则,将返回数组中的所有键。