在二维数组中查找最小/最大值

7

我有一个具有以下格式的数组:

Array
(
    [0] => Array
        (
            [DateTime] => "2013-05-22 14:21:01"
            [Price] => 102.01
        )
    [1] => Array
        (
            [DateTime] => "2013-05-23 15:55:01"
            [Price] => 52.60
        )
    [2] => Array
        (
            [DateTime] => "2013-05-25 14:23:01"
            [Price] => 452.25
        )
    ... etc
)

我需要找到Price的最低和最高值。 min只返回键。我也尝试过max(array_map("max", $data)),但那只返回452.25
我需要手动使用foreach吗?

1
max(array_column($data, 'Price')) 可以给你最高的价格,但你无法获取相关的日期时间。 - mpen
你期望的输出是什么? - qtuan
我只需要实际价格,不需要与任何东西相关联。 - Chuck Le Butt
3个回答

40

以下是获取最小值和最大值的一种方法:

$min = min(array_column($array, 'Price'));
$max = max(array_column($array, 'Price'));
返回嵌套数组的最小值和最大值:
$prices = array_column($array, 'Price');
$min_array = $array[array_search(min($prices), $prices)];
$max_array = $array[array_search(max($prices), $prices)];

因为那似乎是你想要做的事情,所以你可以将每个任务都放在一行中执行:

$min_array = $array[array_search(min($prices = array_column($array, 'Price')), $prices)];
$max_array = $array[array_search(max($prices = array_column($array, 'Price')), $prices)];

需要 PHP >= 5.5.0 才能使用 array_column() 函数,或者使用PHP array_column() 实现

使用 array_map() 函数获取最小值和最大值:

$min = min(array_map(function($a) { return $a['Price']; }, $array));
$max = max(array_map(function($a) { return $a['Price']; }, $array));

也许使用 array_filter() 或者 array_reduce() 函数会更好。


看起来很不错!现在我需要想办法让我的EC2实例运行PHP 5.5 ;) (或者我只是按照你提供的实现方式去做。) - Chuck Le Butt
添加了一种替代方案。 - AbraCadaver

1

我认为最好的方法(最简单的方法)是从您的数组中获取所有价格并存储在单独的数组中。

然后,您可以从新数组中提取最大值。

试试这个:

$myarray = array ( array( "DateTime" => "2013-05-22 14:21:01", "Price" => 102.01),
                   array( "DateTime" => "2013-05-23 15:55:01", "Price" => 52.60),
                   array( "DateTime" => "2013-05-25 14:23:01", "Price" => 452.25)
                 );

//$key is index of $myarray and $value is one of subarrays
//declare a new array to store all prices

$all_price = array();

foreach ($myarray as $key => $value)
{
    //get only "Price" from each row of array (discard "DateTime")
    $all_price[] = $value["Price"];

}

//$all_price cointains now all prices and you can get min and max

$min = min($all_price);
$max = max($all_price);

echo("Min: ".$min."<br />");
echo("Max: ".$max."<br />");

将以下代码复制并粘贴到http://www.writephponline.com/中,即可查看结果! :D
适用于 PHP 版本低于 5.5.0 的情况。

我修改了我的答案,因为我误读了问题。这个解决方案已经经过测试,非常简单。你可以在PHP测试器中尝试它。我留下的链接。 - Yeti82

1
我喜欢使用array_reduce()
$a[]=array('name'=>'kokopiko','price'=>34);
$a[]=array('name'=>'kokospiko2','price'=>234);
$a[]=array('name'=>'kokospiko3','price'=>4);

$minmax = array_reduce($a, function($result, $item) {

    if (!isset($result['min'])) {
        $result['min']=$item;
    }
    if ($result['min']['price'] > $item['price']) {
        $result['min']=$item;
    }

    if (!isset($result['max'])) {
        $result['max']=$item;
    }
    if ($result['max']['price'] < $item['price']) {
        $result['max']=$item;
    }
    return $result; 
}); 

var_dump($minmax);

短版本

$a[]=array('name'=>'kokopiko','price'=>34);
$a[]=array('name'=>'kokospiko2','price'=>234);
$a[]=array('name'=>'kokospiko3','price'=>4);

$init=array('min'=>$a[0],'max'=>$a[0]);

$minmax = array_reduce($a, function($result, $item) {
    ($result['min']['price'] < $item['price'])?:$result['min']=$item;
    ($result['max']['price'] > $item['price'])?:$result['max']=$item;
    return $result; 
}, $init);

仅限最小/最大值(不包括关联数组元素)

$min= array_reduce($a, function($result, $item) {return min($result, $item['price']);}, $a[0]['price']);
$max= array_reduce($a, function($result, $item) {return max($result, $item['price']);}, $a[0]['price']);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接