Hello
Hello

Reputation: 85

PHP - Get maximum key and its value in array

I have a for each loop that loops through a set of dates. How can I get the max value of $key_date?

    $i=0;
    foreach ($data as $key_date => $value_price) 
    {  
        if($key_date>=$start_date && $key_date<=$end_date) 
        {
            if (empty($temp[$i])) {
                $temp[$i]=array($key_date(float)$value_price['price']);
             }
            else {
                 array_push($temp[$i], (float)$value_price['price']);
                 }
              $i++;
        }  
    }

Right now i get all the key_dates and value_prices based on the start and end date.How can I get only the price based on the latest date (max date). So instead of doing

array_push($temp[$i], (float)$value_price['settlement_price']);

I should be able to do array_push latest date between the $start_date and $end_date and its correspnding price

Upvotes: 1

Views: 106

Answers (1)

mrateb
mrateb

Reputation: 2509

To get the maximum key in an array you can use:

$max_key = max(array_keys($array));

You can look here for more details:

http://php.net/manual/en/function.max.php

Upvotes: 1

Related Questions