2011-03-17 76 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) 

所以,你可以看到排列的按键知道如何访问单个值。