How to handle the key (?), to get the value that is stored in a variable?

Hi!

Refer to API of one service, record the response in a variable:
$clientInfo = curl_exec($ch);

For clarity, I deduce the answer:
print_r($clientInfo);

Get:
{"count":1,"data":[{"id":12345,"name":"name","phone":"+88005553535","email":"email@email.com","card":"","birth_date":null,"comment":"","discount":0,"visits":8,"sex_id":0,"sex":"","sms_check":0,"sms_bot":0,"spent":0,"paid":0,"balance":0,"importance_id":0,"importance":"","categories":[],"custom_fields":{}}]}


Question: how do I access the ID key in this... (um, the mountains?), then to write the value of ID (in the example 12345) to the variable $clientID?
April 4th 20 at 13:17
1 answer
April 4th 20 at 13:19
Solution
$clientInfo = curl_exec($ch);
$data = json_decode($clientInfo, true);
if (json_last_error() != JSON_ERROR_NONE) {
 //echo json_last_error_msg();
 throw new \Exception(json_last_error_msg());
}
echo $data['data'][0]['id'];
echo $data['data'][0]['id'];


And can you explain why there is [0]? Turning to the first element inside the "data"? - sheldon71 commented on April 4th 20 at 13:22
@sheldon71, Yes. the "data" array. If there is always one item stupid to take it at index "0". If it arrives a few positions, then processed in a loop. - Mireya.Runolfsdott commented on April 4th 20 at 13:25
@Mireya.Runolfsdott, i.e. take the first element ([0]), and then look for the ['id']? Right?

PS Need to re-read about arrays) - sheldon71 commented on April 4th 20 at 13:28
@sheldon71, right.
First take the key data, the first element (zero), this element's key id. - Mireya.Runolfsdott commented on April 4th 20 at 13:31
@Mireya.Runolfsdott, thank you! - sheldon71 commented on April 4th 20 at 13:34
only not echo a throw new Exception, or at least trigger_error() - Alexandro.Grant26 commented on April 4th 20 at 13:37
@Alexandro.Grant26, Yes, right. - Mireya.Runolfsdott commented on April 4th 20 at 13:40

Find more questions by tags PHP