【问题标题】:Find min/max in a two dimensional array在二维数组中查找最小值/最大值
【发布时间】:2015-04-06 23:27:17
【问题描述】:

我有一个格式如下的数组:

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 并手动完成吗?

【问题讨论】:

  • max(array_column($data, 'Price')) 会给您最高的价格,但您将无法获得相关的 DateTime。
  • 你的预期输出是什么?
  • 我只需要实际价格。不需要与任何东西关联。

标签: php arrays max min array-map


【解决方案1】:

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

$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)];

array_column() 需要 PHP >= 5.5.0 或使用 PHP Implementation of 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 ;) (或者我会得到你链接的实现。)
  • 添加了一个替代方案。
【解决方案2】:

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

然后你从新数组中提取最大值。

试试这个:

$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

也适用于低于 5.5.0 的 PHP

【讨论】:

  • 我编辑了我的答案,因为我误读了这个问题。该解决方案已经过测试,非常简单。您可以在 PHP 测试器中进行尝试。我留下的链接。
【解决方案3】:

我喜欢用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']);

【讨论】:

  • 我希望有人会发布一个,但它的代码很多。
  • 我只是想让它更清楚:) 我会添加一个简短的版本
猜你喜欢
  • 2019-03-31
  • 2018-07-05
  • 2017-04-03
  • 2015-02-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
相关资源
最近更新 更多