1

Possible Duplicate:
How to access an object property with a minus-sign?

I am trying to parse geo:point / geo:lat but getting nowhere fast. Here's the code I have so far

  $content = get_data('http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=united+kingdom&api_key=XXX&format=json&limit=10000&festivalsonly=1');
  $LFMdata = json_decode($content);
  foreach ($LFMdata->events->event as $event) {
      $venue_lat = $event->venue->location["geo:point"]["geo:lat"];
      $venue_long = $event->venue->location["geo:point"]["geo:long"];

The JSON will contain something like

"geo:point": {
     "geo:lat": "52.7352",
     "geo:long": "-1.695392"
}

Anyone got any idea? Seen examples in JavaScript but not PHP

Community
  • 1
  • 1
pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • 1
    What does `var_dump($LFMdata);` return? – Glavić Jan 16 '13 at 12:06
  • 2
    Try `$object->{'geo:point'}->{'geo:lat}` or similar. – halfer Jan 16 '13 at 12:06
  • Are you sure you have a `location` returned as an array? Try to do `var_dump($event->venue->location)` and see what it will return – Viktor S. Jan 16 '13 at 12:06
  • Fangel: `object(stdClass)#6 (5) { ["geo:point"]=> object(stdClass)#7 (2) { ["geo:lat"]=> string(0) "" ["geo:long"]=> string(0) "" } ["city"]=> string(6) "London" ["country"]=> string(14) "United Kingdom" ["street"]=> string(0) "" ["postalcode"]=> string(0) "" } ` – pee2pee Jan 16 '13 at 12:09
  • 1
    @halfer's answer is correct, or return is as an array (json_decode($data, TRUE)). – Toddish Jan 16 '13 at 12:11

1 Answers1

2

You can use

$venue_lat = $event->venue->location->{"geo:point"}->{"geo:lat"};

Or make it return everything like an associative array (second param of json_decode set to true):

 $LFMdata = json_decode($content, true);
  foreach ($LFMdata["events"]["event"] as $event) {
      $venue_lat = $event["venue"]["location"]["geo:point"]["geo:lat"];
      $venue_long = $event["venue"]["location"]["geo:point"]["geo:long"];
Viktor S.
  • 12,736
  • 1
  • 27
  • 52