简体   繁体   中英

How to extract json_decode array element value from cURL

What I am trying to do is to get the value of the first element, [0]['uri'] from my result curl api. I have an associative array from the curl_exec but can't figure out how to extract the data to find the value of the first element. I have tried echo $alerts[0]['uri'] but it doesn't work.

<?php

//  Initiate curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json',"Authorization: Bearer $access_token"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

$alerts = json_decode($result,true);
print_r($alerts);
//echo $alerts[0]['uri'] doesn't work.

?>

Output Result:

Array (
    [result] => success
    [message] => Array (
        [alerts] => Array (
            [0] => Array (
                [id] => 78591963
                [uri] => htps://access.rrr.com/interface/open_api/api/alerts/78591963
            )
            [1] => Array (
                [id] => 78576283
                [uri] => htps://access.rrr.com/interface/open_api/api/alerts/78576283
            )
            [2] => Array (
                [id] => 78576209
                [uri] => htps://access.rrr.com/interface/open_api/api/alerts/78576209
            )
            [3] => Array (
                [id] => 78572644
                [uri] => htps://access.rrr.com/interface/open_api/api/alerts/78572644
            )
            [4] => Array
            )
        )
    )
)

Answer was given by other gentlemen in the comment. I failed to look at the array structure. It is simply: $alerts['message']['alerts'][0]['uri']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM