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

使用php将json对象转换为数组,php,arrays,json,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 );

我想在需要时一个接一个地回显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>";
;

?>
我想按值回显值

尝试以下方法:

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


有关更多详细信息,请查看

我认为这是对象数组$resArr=(数组)$resArr;所以这是正确的way@Madushanka是的,您也可以使用它,但使用php函数
json\u decode
是最好的方法
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
)
$resArr = json_decode($response, true);