我无法在PHP中获取JSON输出

时间:2016-01-08 17:20:18

标签: php arrays json curl

我从curl请求获得了以下响应输出。

Array
(
    [code] => 200
    [message] => Message history obtained successfully.
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 3390257
                    [aggregator_id] => 25
                    [user_id] => 184279092
                    [message_id] => 7784285
                    [ext_message_id] => 152127f68b2000031bf6584fa8494a7b
                    [send_date] => 2016-01-05 09:54:17
                    [dest_address] => 8476099160
                    [dest_country] => 1
                    [dest_type] => 1
                    [source_address] => 8
                    [source_country] => 1
                    [source_type] => 2
                    [message] => Heres  how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
                    [transaction_id] =>
                    [message_status] => 14
                    [message_schedule_id] => 59335
                    [last_modified_date] => 2016-01-05 09:54:52
                    [last_modified_user_id] => 184279092
                    [keyword_id] => 1459
                )

            [1] => Array
                (
                    [id] => 3390261
                    [aggregator_id] => 25
                    [user_id] => 184279092
                    [message_id] => 7784289
                    [ext_message_id] => 152127f6c36000171df5874efba398b4
                    [send_date] => 2016-01-05 09:54:17
                    [dest_address] => 6188892860
                    [dest_country] => 1
                    [dest_type] => 1
                    [source_address] => 8
                    [source_country] => 1
                    [source_type] => 2
                    [message] => Heres  how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
                    [transaction_id] =>
                    [message_status] => 14
                    [message_schedule_id] => 59335
                    [last_modified_date] => 2016-01-05 09:54:52
                    [last_modified_user_id] => 184279092
                    [keyword_id] => 1459
                )

            [2] => Array
                (
                    [id] => 3390265
                    [aggregator_id] => 25
                    [user_id] => 184279092
                    [message_id] => 7784293
                    [ext_message_id] => 152127f65dd000031bf6584fa84949af
                    [send_date] => 2016-01-05 09:54:17
                    [dest_address] => 2032415751
                    [dest_country] => 1
                    [dest_type] => 1
                    [source_address] => 8
                    [source_country] => 1
                    [source_type] => 2
                    [message] => Heres  how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
                    [transaction_id] =>
                    [message_status] => 14
                    [message_schedule_id] => 59335
                    [last_modified_date] => 2016-01-05 09:54:52
                    [last_modified_user_id] => 184279092
                    [keyword_id] => 1459
                )

            [3] => Array
                (
                    [id] => 3390270
                    [aggregator_id] => 25
                    [user_id] => 184279092
                    [message_id] => 7784298
                    [ext_message_id] => 152127f6a2a0001a8d8e4ef0eebbbe5d
                    [send_date] => 2016-01-05 09:54:17
                    [dest_address] => 3106193605
                    [dest_country] => 1
                    [dest_type] => 1
                    [source_address] => 8
                    [source_country] => 1
                    [source_type] => 2
                    [message] => Heres  how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
                    [transaction_id] =>
                    [message_status] => 14
                    [message_schedule_id] => 59335
                    [last_modified_date] => 2016-01-05 09:54:52
                    [last_modified_user_id] => 184279092
                    [keyword_id] => 1459
                )

        )

)

现在我想得到像$keyword_id = 1459这样的数组值但是我无法访问数组。

2 个答案:

答案 0 :(得分:1)

假设此响应存储在名为$response的变量中,以下是访问数组元素的一些示例...

$response['data'][0]['id']            // 3390257 (id of 1st data element)
$response['data'][1]['dest_address']  // 6188892860 (dest_address of 2nd data element)
$response['data'][3]['keyword_id']    // 1459 (keyword_id of 4th data element)

您还可以迭代数据元素并按如下方式打印ID ...

foreach ($response['data'] as $i => $element) {
    echo "The id of element " . $i . " is " . $element['id'] . "\n";
}

答案 1 :(得分:0)

你可以通过foreach循环获得keyword_id。在你的数组上运行foreach循环并通过keyword_id索引获取数据。

foreach($array as $value){
     $keyword_id = $value['keyword_id'];
}

这将为您提供keyword_id。