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.

 

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.

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 :-)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.