使用php将json对象转换为数组

时间:2016-01-14 05:59:48

标签: php arrays json

当我想要

时,我想逐个回显json数组中的值
<?php
    function get_web_page($url) {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,   // return web page
            CURLOPT_HEADER         => false  // don't return headers
        );

        $ch = curl_init($url);
        curl_setopt_array($ch, $options);

        $content  = curl_exec($ch);

        curl_close($ch);

        return $content;
    }
$response = get_web_page("http://maps.googleapis.com/maps/api/geocode/json?address=colombo");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>";
print_r($resArr);
echo "</pre>";
echo "<br>";
echo "<br>";
;

?>

这是代码

上方的浏览器中的当前结果
stdClass Object
(
[results] => Array
    (
        [0] => stdClass Object
            (
                [address_components] => Array
                    (
                        [0] => stdClass Object
                            (
                                [long_name] => Colombo
                                [short_name] => Colombo
                                [types] => Array
                                    (
                                        [0] => locality
                                        [1] => political
                                    )

                            )

                        [1] => stdClass Object
                            (
                                [long_name] => Colombo
                                [short_name] => Colombo
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_2
                                        [1] => political
                                    )

                            )

                        [2] => stdClass Object
                            (
                                [long_name] => Western Province
                                [short_name] => WP
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

                            )

                        [3] => stdClass Object
                            (
                                [long_name] => Sri Lanka
                                [short_name] => LK
                                [types] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

                            )

                    )

                [formatted_address] => Colombo, Sri Lanka
                [geometry] => stdClass Object
                    (
                        [bounds] => stdClass Object
                            (
                                [northeast] => stdClass Object
                                    (
                                        [lat] => 6.9805844
                                        [lng] => 79.8900852
                                    )

                                [southwest] => stdClass Object
                                    (
                                        [lat] => 6.8625113
                                        [lng] => 79.8225192
                                    )

                            )

                        [location] => stdClass Object
                            (
                                [lat] => 6.9270786
                                [lng] => 79.861243
                            )

                        [location_type] => APPROXIMATE
                        [viewport] => stdClass Object
                            (
                                [northeast] => stdClass Object
                                    (
                                        [lat] => 6.9805844
                                        [lng] => 79.8900852
                                    )

                                [southwest] => stdClass Object
                                    (
                                        [lat] => 6.8625113
                                        [lng] => 79.8225192
                                    )

                            )

                    )

                [place_id] => ChIJA3B6D9FT4joRjYPTMk0uCzI
                [types] => Array
                    (
                        [0] => locality
                        [1] => political
                    )

            )

    )

[status] => OK
)

我希望按值回显价值

1 个答案:

答案 0 :(得分:1)

尝试:

$resArr = json_decode($response, true);

它会将其转换为关联数组格式。

有关详细信息,请查看JSON Decode PHP