Jump to content

Please Help Me with JSON and PHP Multi-Dimentional Array


Gunty23

Recommended Posts

Hi,

 

I have been banging my head against a brick wall the past few days trying to figure this out after reading so many tutorial and forum post but I still can't figure it out. I would be very grateful if someone could help me out.

 

Basically I am being supplied with this JSON Data structure:

$json='{
    "countries": {
        "country": [
            {
                "@attributes": {
                    "Name": "Germany",
                    "Lat": "52.6847077739394",
                    "Lng": "-6.81976318359375",
                    "ZoomLevel": "7"
                }
            },
            {
                "@attributes": {
                    "Name": "Sweeden",
                    "Lat": "54.00131186464818",
                    "Lng": "-7.36358642578125",
                    "ZoomLevel": "8"
                }
            }
        ]
    }
}';

 

I decode the JSON data structure as a PHP array and I print out the contents of the array for debugging purposes:


$json_array=json_decode($json,true);

print_r($json_array);

 

Heres what the outputed array looks like:

Array ( 
			[countries] => Array ( 
					[country] => Array ( 
							[0] => Array ( 
											[@attributes] => Array ( 
											[Name] => Germany 
											[Lat] => 52.6847077739394 
											[Lng] => -6.81976318359375 
											[ZoomLevel] => 7 	   )
									     )

							[1] => Array (  [@attributes] => Array ( 
											[Name] => Sweeden 
											[Lat] => 54.00131186464818 
											[Lng] => -7.36358642578125 
											[ZoomLevel] => 8 		) 
										 )
								      )
								)
			)

 

I want to be able to access all  the country names contained in [Name]

 

Can anyone please help me on how to do this correctly as I've tried lots of code with multi-dimensional arrays but I've had no luck. I am not able to acces the variable correctly.

 

Thanks in advance.

 

Link to comment
Share on other sites

I spent a bit of time trying to figure this one out as well, can't seem to get a grasp on it, was running through array_combine, array_values, and array_keys like mad. :-/, Though after searching I think what you're looking for is array flattening, though, maybe not.

 

http://stackoverflow.com/questions/1455758/find-all-second-level-keys-in-multi-dimensional-array-in-php

As well as:

http://efreedom.com/Question/1-1971327/PHP-Trying-Pull-Values-Multidimensional-Recursive-Array

 

Might be of use to you.

Link to comment
Share on other sites

Hi Zurev,

 

Thanks for the response,

 

I've finally just got it working after a lot of hassle!

 

Here is the code that works:

 

foreach ($json_array['countries']['country'] as $row) { 

		echo $row['@attributes']['Name']; 
		}

 

Thanks for your help  with the links :-)

Link to comment
Share on other sites

Hi Zurev,

 

Thanks for the response,

 

I've finally just got it working after a lot of hassle!

 

Here is the code that works:

 

foreach ($json_array['countries']['country'] as $row) { 

		echo $row['@attributes']['Name']; 
		}

 

Thanks for your help  with the links :-)

 

That's upsetting, because looking at that, it was extremely simple.... /facepalm.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.