/    Sign up×
Community /Pin to ProfileBookmark

json_decode Usage with multi dimension JSON code

I am having proble to extract a vlaue from this code

[QUOTE]

{ “name”: “1600 Amphitheatre Parkway, Mountain View, CA”, “Status”: { “code”: 200, “request”: “geocode” }, “Placemark”: [ { “id”: “p1”, “address”: “1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA”, “AddressDetails”: {“Country”: {“CountryNameCode”: “US”,”CountryName”: “USA”,”AdministrativeArea”: {“AdministrativeAreaName”: “CA”,”Locality”: {“LocalityName”: “Mountain View”,”Thoroughfare”:{“ThoroughfareName”: “1600 Amphitheatre Pkwy”},”PostalCode”: {“PostalCodeNumber”: “94043”}}}},”Accuracy”: 8}, “ExtendedData”: { “LatLonBox”: { “north”: 37.4251196, “south”: 37.4188244, “east”: -122.0809954, “west”: -122.0872906 } }, “Point”: { “coordinates”: [ -122.0841430, 37.4219720, 0 ] } } ] }

[/QUOTE]

I want to extract this value
-122.0841430

Using PHP json_decode (object or array), json_decode works fine for me with simple files but it doesnt work here for me here, any ideas?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordApr 08.2009 — You could try something like this. (Fast and dirty) or look into recursion instead of the nested foreach loop.

[code=php]
<?php
$json = '{
"name": "1600 Amphitheatre Parkway, Mountain View, CA", "Status":
{
"code": 200, "request": "geocode"
},
"Placemark": [
{
"id": "p1", "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "AddressDetails":
{
"Country":
{
"CountryNameCode": "US","CountryName": "USA","AdministrativeArea":
{
"AdministrativeAreaName": "CA","Locality":
{
"LocalityName": "Mountain View","Thoroughfare":
{
"ThoroughfareName": "1600 Amphitheatre Pkwy"
}
,"PostalCode":
{
"PostalCodeNumber": "94043"
}
}
}
}
,"Accuracy": 8
}
, "ExtendedData":
{
"LatLonBox":
{
"north": 37.4251196, "south": 37.4188244, "east": -122.0809954, "west": -122.0872906
}
}
, "Point":
{
"coordinates": [ -122.0841430, 37.4219720, 0 ]
}
}
]
}';


$arr = json_decode($json, true);

foreach($arr as $key1 => $value1) {
echo "<b>Key1: </b>" . $key1 . " <b>value1: </b>" . $value1 . "<br />";

if(is_array($value1)) {
foreach($value1 as $key2 => $value2) {
echo "<b>Key2: </b>" . $key2 . " <b>value2: </b>" . $value2 . "<br />";

if(is_array($value2)) {
foreach($value2 as $key3 => $value3) {
echo "<b>Key3: </b>" . $key3 . " <b>value3: </b>" . $value3 . "<br />";

if(is_array($value3)) {
foreach($value3 as $key4 => $value4) {
echo "<b>Key4: </b>" . $key4 . " <b>value4: </b>" . $value4 . "<br />";

if(is_array($value4)) {
foreach($value4 as $key5 => $value5) {
echo "<b>Key5: </b>" . $key5 . " <b>value5: </b>" . $value5 . "<br />";
}
}
}
}
}
}
}
}

}
?>
[/code]
×

Success!

Help @wisam74us spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...