从数组返回单个变量

发布于 2024-10-22 07:59:46 字数 838 浏览 1 评论 0原文

我正在使用以下脚本:

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

要获取邮政编码的详细信息,我使用以下命令:

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

这将返回“$zip”的所有详细信息:

32.9116,-96.7323 ,Dallas,Dallas,TX,Texas,214,Central

任何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.

不幸的是,我无法弄清楚如何获取单个值(城市)。将不胜感激任何帮助!

谢谢!

i am using following script:

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

To get the details for a ZIP code, I am using this:

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

This returns all details of "$zip" :

32.9116,-96.7323,Dallas,Dallas,TX,Texas,214,Central

Could any1 help me to make the script return ONLY the city variable? The FAQ of the script, says the following:

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.

Unfortunately I cant figure out how to get a single value (city). Would appreciate any help!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素手挽清风 2024-10-29 07:59:46

这些函数返回一个数组,因此我们必须将其存储在变量中。

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

接下来,我们可以选择指向城市的键。钥匙也称为城市。

  echo $selected['city'];

The functions is returning an array, so we have to store it in a variable.

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

Next, we can select the key which points to the city. The key is also called city.

  echo $selected['city'];
愚人国度 2024-10-29 07:59:46

更改此行

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

$result = $selected['city'];

Change this line

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

To

$result = $selected['city'];
起风了 2024-10-29 07:59:46

为了将来参考,implode() explode() 等都是数组函数。因此,如果您在某些东西上使用它们,那么您可以 print_r() 它来查看其结构。

如果您执行了 print_r($selected);var_dump($selected);,您将得到类似这样的输出

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

:数组以了解如何访问各个值。

For future reference, implode() explode() etc. are array functions. So, if you're using them on something then you can print_r() it to see its structure.

If you had done print_r($selected); or var_dump($selected);, you would have gotten something like this as output:

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

So you can see the keys of the array to know how to access the individual values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文