簡體   English   中英

從數組獲取前兩個值並運行foreach

[英]Get first two values from an array and run foreach

我有以下所示格式的json數據

在此處輸入圖片說明

我正在嘗試使用foreach for API構建URL,該API需要將開始和結束日期作為參數。

這是url的示例-> https://api.website.com/?action=export_ranking&startDate=2014-04-08&stopDate=2019-02-20

我的問題是,如何建立鏈接對於API拉動開始和結束日期從JSON數據。

我正在使用PHP BTW。

非常感謝您的幫助。

您可以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 } 

@Khoa的答案是正確的。 這是PHP版本:

for($i = 0; $i < count($arr)-1; $i++) {
    $urls[] = "https://api.website.com/?action=export_ranking&startDate=" . $arr['dates'][$i]['date'] . "&stopDate=" . $arr['dates'][$i+1]['date'];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM