2011-03-17 42 views
0

我使用下面的腳本:返回單個變量從陣列

http://www.micahcarrick.com/php-zip-code-range-and-distance-calculation.html

要獲得一個郵政編碼的細節,我用這:

$selected = $z->get_zip_details($zip); 
$result = implode(",", $selected); 
echo $result; 

這將返回的所有細節「$拉鍊」:

32.9116,-96.7323,達拉斯,達拉斯,德克薩斯州,得克薩斯州,214,中央

任何1都可以幫助我使腳本只返回城市變量?該腳本的常見問題解答如下:

get_zip_details($zip) 

Returns the details about the zip code: $zip. Details are in the form of a keyed array. The keys are: latitude, longitude, city, county, state_prefix, state_name, area_code, and time_zone. All are pretty self-explanitory. Returns false on error. 

不幸的是我無法弄清楚如何獲得單個值(城市)。將不勝感激任何幫助!

謝謝!

回答

1

函數返回一個數組,所以我們必須將它存儲在一個變量中。

$selected = $z->get_zip_details($zip); 

接下來,我們可以選擇指向城市的鑰匙。關鍵也叫城市。

echo $selected['city']; 
+0

非常感謝!我試圖做echo $ get_zip_details ['city'];以及更多!它現在工作正常,我會記住它的未來:) – Andrej 2011-03-17 10:20:23

1

改變這一行

$result = implode(",", $selected);


$result = $selected['city'];
+0

這一個作品也:)謝謝你也Michiel! – Andrej 2011-03-17 10:20:48

+0

@安德烈:不客氣! – 2011-03-17 10:30:55

0

供將來參考,implode()explode()等是數組函數。所以,如果你正在使用它們,那麼你可以print_r()它看到它的結構。

如果你做了print_r($selected);var_dump($selected);,你會得到這樣的事情作爲輸出:

Array ([x] => 32.9116 [y] =>-96.7323 [city] => Dallas [state] => Texas [etc] => etc) 

所以,你可以看到排列的按鍵知道如何訪問單個值。