1

可能重复:
如何使用减号访问对象属性?

我正在尝试解析 geo:point / geo:lat 但无处可去。这是我到目前为止的代码

  $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"];

JSON 将包含类似

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

有人知道吗?在 JavaScript 中看到了示例,但在 PHP 中没有看到

4

1 回答 1

2

您可以使用

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

或者让它像关联数组一样返回所有内容(json_decode设置为的第二个参数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"];
于 2013-01-16T12:13:19.343 回答