0

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
                        )
1
  • I can't see how you access predictions in your code snipped. Could it be the error is somewhere else? Commented Dec 31, 2017 at 10:51

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hard to be sure without seeing the original JSON but I think maybe the foreach loop needs to iterate like this: foreach ($jsondecode2->predictions as $jsonforeach2) { $Country = ($jsonforeach2->description->structured_formatting->secondary_text); } - That said, I'd stick with the array method, much simpler if you ask me! :)
The PHP docs are really good (and not as daunting as you may think!) the page on foreach is here: php.net/manual/en/control-structures.foreach.php - It's pretty readable and remember to scroll down to see examples. I've also updated my answer with example foreach loops in Object and Array style, hopefully that helps make it clearer.

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.