I have an array with the following format:
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
)
I need to discover the lowest and highest value of Price
.
min
only returns they key. I've also tried max(array_map("max", $data))
but that only returns 452.25
.
Will I have to use a foreach
and do it manually?
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