0

I have a multidimensional array coming from JSON. I cannot seem to get to print one value from all. It either gives me nothing or says array. I need to print all the zip_code fields.

 $spit = json_decode($result);

Yields:

stdClass Object
   (
  [zip_codes] => Array
    (
        [0] => stdClass Object
            (
                [zip_code] => 33779
                [distance] => 9.513
                [city] => Largo
                [state] => FL
            )

        [1] => stdClass Object
            (
                [zip_code] => 33771
                [distance] => 9.188
                [city] => Largo
                [state] => FL
            )

        [2] => stdClass Object
            (
                [zip_code] => 33760
                [distance] => 9.989
                [city] => Clearwater
                [state] => FL
            )

        [3] => stdClass Object
            (
                [zip_code] => 33770
                [distance] => 8.525
                [city] => Largo
                [state] => FL
            )

        [4] => stdClass Object
            (
                [zip_code] => 33786
                [distance] => 8.153
                [city] => Belleair Beach
                [state] => FL
            )

        [5] => stdClass Object
            (
                [zip_code] => 33764
                [distance] => 7.651
                [city] => Clearwater
                [state] => FL
            )

        [6] => stdClass Object
            (
                [zip_code] => 33756
                [distance] => 6.373
                [city] => Clearwater
                [state] => FL
            )

I have tried to access with $zip=$spit->zip_codes->zip_code; and $zip=['zip_codes']['zip_code'];

I am sure I am just missing the obvious but have tried about 20 ways to echo it out with no luck.

6
  • 1
    Try $zip = $spit->zip_codes[0]->zip_code; Commented May 4, 2016 at 20:30
  • This give me only the first zip code not the whole array. Commented May 4, 2016 at 20:57
  • Possible duplicate of Accessing JSON array after json_decode/multidimensional array Commented May 4, 2016 at 20:57
  • 1
    So you loop through and access $spit->zip_codes[$i]->zip_code instead. Commented May 4, 2016 at 20:58
  • I gave you a hint to access one zip code. Just loop through it if you want all the zip codes. Commented May 4, 2016 at 21:01

1 Answer 1

0

Change

$spit = json_decode($result);

to

$spit = json_decode($result,1);

This will convert JSON into an array and then you can print the zip code as

print_r($spit["zip_codes"]);
Sign up to request clarification or add additional context in comments.

Comments

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.