Issue
I have an cURL file that will return an array like below and I wonder how can I declare the variable of [data]=>[id].I tried like $decoded.data or $decoded.[data] but it does not work.
Array
(
[data] => Array
(
[id] => 2
[email] => [email protected]
[first_name] => Janet
[last_name] => Weaver
[avatar] => https://reqres.in/img/faces/2-image.jpg
)
[support] => Array
(
[url] => https://reqres.in/#support-heading
[text] => To keep ReqRes free, contributions towards server costs are appreciated!
)
)
PHP file:
<?php
$ch = curl_init();
$url = "https://reqres.in/api/users/2";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$decoded = json_decode($resp,true);
print_r($decoded);
curl_close($ch);
?>
Solution
$decoded['data']
$decoded['data']['id']
is the syntax. But the endpoint must return the array that you have printed with json_encode()
Answered By - Valeriu Ciuca Answer Checked By - Robin (WPSolving Admin)