我正在尝试解析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
答案 0 :(得分: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"];