我有循環開始,我可以使用一些幫助。PHP循環覆蓋數組
我不得不通過一些curl調用API來獲取記錄然後推送到另一個API。每個來自這個端點的調用只返回30條記錄,所以如果有超過30條記錄,我也需要循環訪問。
我想在所有循環結束時轉儲出一個數組,以查看我認爲會是什麼數據,但它只輸出1條記錄。
我在這裏錯過了什麼愚蠢的東西?
$project_ids = array(
'111111',
'222222',
);
$array = array();
foreach ($project_ids as $proj_id) {
$go_again = true;
$page = 1;
$per_page = 30;
while ($go_again) {
$curl = curl_init($baseurl . $key_url);
$keyPOSTdata = array(
"date_format" => "d-m-Y",
"page" => $page,
"projects_id" => $proj_id,
);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($keyPOSTdata));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
$result = json_decode($response);
$total = $result->body->total_count;
$new_total = $total - ($per_page * $page);
if($total - ($per_page * $page) > 0) {
++$page;
}
else {
$page = 1;
$go_again = false;
}
curl_close($curl);
foreach ($result->body as $item) {
$array['attribute1'] = $item->attribute1;
$array['attribute2'] = $item->attribute2;
$array['attribute3'] = $item->attribute3;
}
}
}
echo '<pre>';
print_r($array);
echo '</pre>';
exit();
啊是的,現在有道理爲什麼重寫正在發生哈。然而,當改變爲這種方法時,我的輸出沒有任何變化..仍然只有一個項目正在傾倒... – thatryan
我認爲你不是遍歷響應的每一個元素,只是在一個元素的主體上。 while循環中可能需要兩個'foreach'。你能否在你的問題中編輯樣本回應? – ishegg
所以我通過將新的$ record temp數組和$ array []賦值移動到foreach循環中來實現它... – thatryan