0

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!

3 Answers 3

1

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I have tried to do echo $get_zip_details['city']; and much more! It now works fine and I will remeber it for the future :)
1

Change this line

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


To

$result = $selected['city'];

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.