Find min/max in a two dimensional array

放肆的年华 提交于 2019-11-28 02:03:27

Here's one way to get the min and max values:

$min = min(array_column($array, 'Price'));
$max = max(array_column($array, 'Price'));

To return the nested array for the min and max:

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

You could do each in one line since that looked like what you were trying to do:

$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 needed for array_column() or use the PHP Implementation of array_column().

Using array_map() to get just the min and max:

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

There's probably a good array_filter() or array_reduce() as well.

I like to use the 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);

shorter version

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

Only min/max values ( not associated array elements

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

I think best way (easiest way) is get all prices from your array and store in a separate arrays.

Then you extract the maximum value from the new array.

Try this:

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

Copy and paste this code into http://www.writephponline.com/ to see the result! :D

Works also on PHP lower than 5.5.0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!