Deadpool
Deadpool

Reputation: 204

How to put this Json Data into a Php Variable?

I´m having problems with json, I don´t get why my code doesn´t work. I want to put the [secondary_text] => United Kingdom into a php variable, but I´m getting this Notice all the time:

Notice: Trying to get property 'predictions' of non-object in C:.........\CCPSeven.php on line 153

My Code:

  header('Content-Type: application/json');
    $htmlj = file_get_html('https://maps.googleapis.com/maps/api/place/queryautocomplete/json?key=*****&input=London&language=en',false);
    $jsondecode2 = json_decode($htmlj);
        foreach ($jsondecode2 as $jsonforeach2) {
        $Country = ($jsonforeach2->description->structured_formatting->secondary_text);
    }
    print_r($Country);

The Json:

stdClass Object
(
    [predictions] => Array
        (
            [0] => stdClass Object
                (
                    [description] => London, United Kingdom
                    [id] => *****+
                    [matched_substrings] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [length] => 6
                                    [offset] => 0
                                )

                        )

                    [place_id] => ChIJdd4hrwug2EcRmSrV3Vo6llI
                    [reference] => *****
                    [structured_formatting] => stdClass Object
                        (
                            [main_text] => London
                            [main_text_matched_substrings] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [length] => 6
                                            [offset] => 0
                                        )

                                )

                            [secondary_text] => United Kingdom
                        )

Upvotes: 0

Views: 52

Answers (1)

Jonathan
Jonathan

Reputation: 454

You might find it a bit simpler to decode the JSON string to a PHP associative array rather than an object:

$jsondecode2 = json_decode($htmlj, true);
$Country = $jsondecode2['predictions'][0]['structured_formatting']['secondary_text'];

The 2nd option on the json_decode function tells it to return an array rather than an object. More info here: http://php.net/manual/en/function.json-decode.php

In your sample code, looks like maybe you've missed off the ['predictions'] bit in your foreach loop? Here are two examples for Object and Array style:

// Object style
$jsondecode2 = json_decode(file_get_contents($htmlj));
foreach ($jsondecode2->predictions as $jsonforeach2) {
    $Country = $jsonforeach2->structured_formatting->secondary_text;
    print PHP_EOL . "Object style: Country: " . $Country;
}

// Associative array style
$jsondecode2 = json_decode(file_get_contents($htmlj), true);
foreach ($jsondecode2['predictions'] as $jsonforeach2) {
    $Country = $jsonforeach2['structured_formatting']['secondary_text'];
    print PHP_EOL . "Array style: Country: " . $Country;
}

Upvotes: 1

Related Questions