2

I have been trying to do this for awhile. I have a request to an api using cURL. For the moment I can execute the cURL function and encode it with json. I can print_r() an element of the array which gives me back one result. However I have a return from the api of 25 elements of which I need to return back just the id of the 25 elements. I have included a var_dump() of the array to give you a screen shot of the array. The list iterates to 24 elements. I need the 25 Id's

Below is my code

<?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"
    }
  }

1 Answer 1

2

Loop through the $data_result_base_user['items'], save ids in array and return that array

$ids = array();

foreach ($data_result_base_user['items'] as $key => $value) {
    $ids[]=$value['data']['id']; // assuming all element have same structure
}

return $ids;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.