1

I've got an array that holds the results of a mysql query for zipcodes form a certain point around a given radius. If I var_dump the array it looks like this:

array(7) { 
[0]=> array(1) { ["postal_code"]=> string(5) "11510" }
[1]=> array(1) { ["postal_code"]=> string(5) "11518" } 
[2]=> array(1) { ["postal_code"]=> string(5) "11520" } 
[3]=> array(1) { ["postal_code"]=> string(5) "11558" }
[4]=> array(1) { ["postal_code"]=> string(5) "11563" } 
[5]=> array(1) { ["postal_code"]=> string(5) "11570" } 
[6]=> array(1) { ["postal_code"]=> string(5) "11572" } 
}

I'd like to extract the data and end up with a string of zipcodes with a comma as delimiter (i.e., "11510,11518,11520,11558,11563,11570,11572").

What would be the best way to do so?

Kara
  • 6,115
  • 16
  • 50
  • 57
zargosh
  • 31
  • 4
  • See: http://stackoverflow.com/q/1494953/3933332 and http://stackoverflow.com/q/7490488/3933332 – Rizier123 Jun 05 '16 at 21:04
  • 2
    $zips = implode(',', array_column($originalData, 'postal_code')); – Mark Baker Jun 05 '16 at 21:26
  • Thanks, Mark! That works perfectly! I had tried both implode and array_column separately, to no avail. Your combination of the two makes perfect sense. Made my night! – zargosh Jun 06 '16 at 05:13

1 Answers1

0

Would something like this do what you want?

$postcodes = array();
foreach ($array as $postcodeData) {
    $postcodes[] = $postcodeData['postal_code'];
}

A foreach loop allows you to loop through the first level of the array, and then because the key is always the same you can just add the postcode to a new array.

Brett
  • 1,951
  • 2
  • 28
  • 35
  • Thanks, Brett. I appreciate it. I should have mentioned that I tried a foreach loop earlier. It returns the data in an array and I was looking to get it into a delimited string. Mark's answer above worked perfectly. – zargosh Jun 06 '16 at 05:14