2

I want to echo value in the json array one by one when i want

<?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>";
;

?>

This is the current result in the browser above code

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
)

I want to echo value by value

1 Answer 1

2

try it with:

$resArr = json_decode($response, true);

It will convert it into associative array format.

For more detail have a look at JSON Decode PHP

Sign up to request clarification or add additional context in comments.

2 Comments

i think it is object array... $resArr=(array)$resArr; so this is the right way
@Madushanka yes you can use that also, bu using php function json_decode is best way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.