1

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

4
  • Do you have sample data please? Commented Oct 20, 2017 at 10:08
  • From where do you get your data? Commented Oct 20, 2017 at 10:11
  • Check php.net/manual/en/function.max.php, there is an example with DateTime objects. Commented Oct 20, 2017 at 10:13
  • Check this answer if valuable for your case. Commented Oct 20, 2017 at 13:13

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

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.