我有以下代码。我试图从请求的提要中获取城市的名称,并将其转换为新的数组。任何人都可以给我一个指示。从json响应中获取城市数组
$city = $_GET['city'];
$json = @file_get_contents('http://ws.geonames.org/searchJSON?country=GB&maxRows=10&name_startsWith=$city');
$json = utf8_encode($json);
$city_suggest = json_decode($json, true);
foreach($city_suggest['geonames'] as $city){
$cities = $city['geonames']['name'];
}
print_r ($cities);
编辑 - 1线JSON响应
{"totalResultsCount":323,"geonames":[{"countryName":"United Kingdom","adminCode1":"ENG","fclName":"city, village,...","countryCode":"GB","lng":-0.12883186340332,"fcodeName":"capital of a political entity","toponymName":"London","fcl":"P","name":"London","fcode":"PPLC","geonameId":2643743,"lat":51.5005149421307,"adminName1":"England","population":7556900},
编辑的 - 的var_dump
array(2) { ["totalResultsCount"]=> int(0) ["geonames"]=> array(0) { } }
你应该告诉我们$ city_suggest – 2010-07-08 17:50:54
是做什么的(我相信你有问题;-))'var_dump(json_decod e($ json,true))' 可能是因为file_get_contents使用单引号而不是双引号。 – 2010-07-08 18:01:20
你的json调用是错误的,你传递$ city作为URL的一部分,而不是将它连接到最后。 – 2010-07-08 18:03:04