Search code examples
phparraysjsonapiawr

Get first two values from an array and run foreach


I've json data in the format shown below

enter image description here

I'm trying to build urls using foreach for API that requires start and finish date as parameters.

Here's the example of url -> https://api.website.com/?action=export_ranking&startDate=2014-04-08&stopDate=2019-02-20

My question is, how do I build urls for API that pulls start and finish date from the json data.

I'm working with PHP BTW.

Any help is highly appreciated.


Solution

  • You can use for:

    var yourUrls; // new array
    var yourDates = yourArray[dates];
    for (i = 0; i < yourDates.length - 1; i++) {
      yourUrls.push("https://api.website.com/?action=export_ranking&startDate=" + yourDates[i].date + "&stopDate=" + yourDates[i + 1].date);
      // add the generated url to yourUrls
    }