letsCode
letsCode

Reputation: 3044

Getting the MAX/MIN from a PHP array that has objects

I have a PHP array that is filled like so:

[
    {"soldPrice":"228.96","dateSold":"05\/22\/2020"},
    {"soldPrice":"204.99","dateSold":"06\/22\/2020"},
    {"soldPrice":"399.99","dateSold":"08\/12\/2020"},
    {"soldPrice":"350.00","dateSold":"08\/23\/2020"}
]

I was able to find the max by doing max($arr);, but now I added the dateSold. How can I find the min/max of this array, but also get the date that it sold?

It would echo 06/22/2020: 204.99 for min.

It would echo 08/22/2020: 399.99 for max.

I tried to add a function like this just to get the max.

function max_attribute_in_array($data_points, $value='soldPrice'){
    $max=0;
    foreach($data_points as $point){
        if($max < (float)$point->{$value}){
            $max = $point->{$value};
        }
    }
    return $max;
}
$max = max_attribute_in_array($mainResponse);
var_dump($max);

but this was a no go.

This just returned int(0)

Upvotes: 0

Views: 327

Answers (3)

Sascha A.
Sascha A.

Reputation: 4626

Iterate with foreach over it and get min/max and remember the corresponding dates.

<?php
$data=[
    ["soldPrice"=>228.96,"dateSold"=>"05\/22\/2020"],
    ["soldPrice"=>204.99,"dateSold"=>"06\/22\/2020"],
    ["soldPrice"=>399.99,"dateSold"=>"08\/12\/2020"],
    ["soldPrice"=>350.00,"dateSold"=>"08\/23\/2020"]];

$min = INF;
$max = -INF;
foreach ($data as $elem) {
    if ($elem['soldPrice'] > $max) {
        $max = $elem['soldPrice'];
        $maxDate = $elem['dateSold'];
    } elseif ($elem['soldPrice'] < $min) {
        $min = $elem['soldPrice'];
        $minDate = $elem['dateSold'];
    }
}

echo 'Max: ' . $max . ' => ' . $maxDate . '<br>';
echo 'Min: ' . $min . ' => ' . $minDate . '<br>';

This delivers:

Max: 399.99 => 08\/12\/2020
Min: 204.99 => 06\/22\/2020

Upvotes: 0

Rudu
Rudu

Reputation: 15892

From your example (which possibly has a typo since there's no 08/22/2020 date), it looks like you want the max of a key from the arrays which are within the overall array (in JS you'd say an array of objects).. which you've almost solved:

<?php
$data=[
    ["soldPrice"=>228.96,"dateSold"=>"05/22/2020"],
    ["soldPrice"=>204.99,"dateSold"=>"06/22/2020"],
    ["soldPrice"=>399.99,"dateSold"=>"08/12/2020"],
    ["soldPrice"=>350.00,"dateSold"=>"08/23/2020"]];

function max_attribute_in_array($arr, $key) {
    $max=null;
    foreach ($arr as $row) {
        if ($row[$key]>$max) {
            $max=$row[$key];
        }
    }
    return $max;
}
function min_attribute_in_array($arr, $key) {
    $min=count($arr)>0 ? $arr[0][$key] : null;
    foreach ($arr as $row) {
        if ($row[$key]<$min) {
            $min=$row[$key];
        }
    }
    return $min;
} 

$maxSoldPrice=max_attribute_in_array($data, 'soldPrice');
$maxDateSold=max_attribute_in_array($data, 'dateSold');
echo $maxSoldPrice.", ".$maxDateSold."\n";

$keys=['soldPrice','dateSold'];
$mins=[];
foreach ($keys as $key)
    $mins[$key]=min_attribute_in_array($data,$key);
print_r($mins);

Which should output

399.99, 08/23/2020
Array
(
    [soldPrice] => 204.99
    [dateSold] => 05/22/2020
)

I've switched the data to PHP arrays, what you've specified as the source looks like JSON, but you can convert JSON to PHP arrays using json_decode($json,true);. Note you could also iterate over the keys of each $row and build the max of each key that way (it would require some structural changes to max_attribute_in_array) - instead of having to specify each key. I've used an iteration approach for the min version just to demonstrate - you could use either (but probably best to be consistent)

Upvotes: 1

trincot
trincot

Reputation: 350881

This gives you the complete entry of the array that has the maximum value:

function max_attribute_in_array($data_points, $value='soldPrice') {
    $col = array_column($data_points, $value);
    return $data_points[array_search(max($col), $col)];
}

Upvotes: 2

Related Questions