我有以下代碼。我試圖從請求的提要中獲取城市的名稱,並將其轉換爲新的數組。任何人都可以給我一個指示。從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