0

I have this array:

Array
(
    [1] => Array
        (
            ['s_date'] => 1/1/1989
            ['e_date'] => 6/30/1989
            ['rate'] => 7.92
        )

    [2] => Array
        (
            ['s_date'] => 7/1/1989
            ['e_date'] => 12/31/1989
            ['rate'] => 8.18
        )

    [3] => Array
        (
            ['s_date'] => 1/1/1990
            ['e_date'] => 6/30/1990
            ['rate'] => 7.14
        )

So I want to search for rate between two given dates but I am not sure how to go about with that, i looked into array_search function but it didn't help.

function findRate($startDate, $endDate) {
  // not sure here what needs to be done...
}

Thanks for the help

8
  • 5
    At Stack Overflow you are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a Minimal, Complete, and Verifiable example. I suggest reading How to Ask a good question and the perfect question. Also, be sure to take the tour and read this. Commented Apr 25, 2017 at 14:07
  • @dev02 what you have tried please show us ? Commented Apr 25, 2017 at 14:08
  • @JohnConde: Yes I do try (if you see my previous answers/questions) but iam totally lost at this one so thought of getting help from SO community :( Commented Apr 25, 2017 at 14:08
  • @JohnConde You missed this stackoverflow.com/questions/43612635/dropdown-mysql-php Is there a place where i can copy&paste your comment (seems generic) with links? Commented Apr 25, 2017 at 14:08
  • 4
    @JustOnUnderMillions I created a pastebin for it. Happy copy and pasting! Commented Apr 25, 2017 at 14:11

1 Answer 1

1

Only to give you a direction, you question is a little unclear, when it comes to your function declaration. Why 2 dates for the lookup?

So i did this

function findRate($date,$array){
 $rate=null;
 foreach($yourArray as $value){
  $s=date('Y-m-d',strtotime($value['s_date']));
  $e=date('Y-m-d',strtotime($value['e_date']));
  if($s<=$lookup && $lookup<=$e){
    $result = $value['rate'];
  }
 }
 return $rate;
}

$rate = findRate('1989-02-02',$yourarrayfromabove);//Result  8.18

Try to understand everthing i do here and extent and change if needed.

And open the next question whit self written code, thnx

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.