如何从cURL中提取json_decode数组元素值

时间:2017-01-15 07:25:55

标签: php arrays json curl

我要做的是从我的结果curl api中获取第一个元素[0]['uri']的值。我有curl_exec的关联数组,但无法弄清楚如何提取数据以找到第一个元素的值。我试过了echo $alerts[0]['uri'],但它没有用。

<?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.

?>

输出结果:

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
            )
        )
    )
)

1 个答案:

答案 0 :(得分:0)

评论中的其他绅士给出了答案。 我没有看到阵列结构。它很简单: $alerts['message']['alerts'][0]['uri']