我一直试图这样做。我有一个使用cURL的api请求。目前我可以执行cURL函数并使用json对其进行编码。我可以print_r()一个数组元素,它给我一个结果。但是我从25个元素的api返回,我需要返回25个元素的id。我已经包含了一个数组的var_dump()来给你一个数组的屏幕截图。该列表迭代为24个元素。我需要25 Id&#39>
以下是我的代码
<?php
$url = 'https://api.getbase.com/v2/lead_sources?sort_by=name:desc';
//Initiate cURL.
$chbase_user = curl_init($url);
curl_setopt($chbase_user, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chbase_user, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4','Accept: application/json','Authorization: Bearer inserted here'));
$result_base_user = curl_exec($chbase_user);
$data_result_base_user = json_decode($result_base_user,true);
echo '<pre>';
print_r($data_result_base_user['items'][0]['data']['id']);
var_dump($data_result_base_user );// Below is the result of this
?>
array(2) {
["items"]=>
array(25) {
[0]=>
array(2) {
["data"]=>
array(6) {
["id"]=>
int(154808)
["created_at"]=>
string(20) "2015-11-09T09:25:55Z"
["updated_at"]=>
string(20) "2018-03-08T16:43:31Z"
["name"]=>
string(17) "X - Zopim IM Chat"
["creator_id"]=>
int(675478)
["resource_type"]=>
string(4) "lead"
}
["meta"]=>
array(1) {
["type"]=>
string(6) "source"
}
}
答案 0 :(得分:2)
循环遍历$data_result_base_user['items']
,在数组中保存id并返回该数组
$ids = array();
foreach ($data_result_base_user['items'] as $key => $value) {
$ids[]=$value['data']['id']; // assuming all element have same structure
}
return $ids;