0

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)

5
  • What do you mean by "this was a no go"? Did you get an error? Did you get some output that you can show us? Commented Aug 23, 2020 at 21:18
  • update... sorry, forgot that. @IMSoP Commented Aug 23, 2020 at 21:21
  • You can just do max(array_column($data_points, $value)). Commented Aug 23, 2020 at 21:28
  • @trincot this doesnt give me the date it belongs to. Commented Aug 23, 2020 at 21:32
  • See my answer to get that also. Commented Aug 23, 2020 at 21:32

3 Answers 3

2

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)];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.