I have a script that I am using to retrieve zip codes within a certain radius using a foreach php statement. However, with these zip codes that I am getting, I want to add a comma between each value (for example: 11111,22222,33333) to use in a SQL query using the command "IN". I have figured out how to get a comma but for some reason, it is putting a comma before the first zip code which is causing my next SQL statement to break.

foreach ($zips as $key => $value) {
	   $space = ",";
	   $fields = $space.$key;
	  echo "$fields";
	}

This is the output I get:
,11111,22222,33333

It is mostly right as there is no comma at the end.
This is the query I am trying to run as well which keeps breaking:

SELECT * FROM customers WHERE zip IN ($fields)

Any help would be greatly appreciated or if there is somehow a way where you can run the query with just a space between the values.

Recommended Answers

All 7 Replies

GROUP_CONCAT(zip SEPARATOR ', ')

edit: Sorry! First post and I messed up. Give me a few minutes and I'll get you a correct answer.

edit II: If you're grabbing those ZIP codes from a DB to start with, the code above works.

/**
 * Format a provided array into human-readable format, separating by $separator, and using $lastSeparator for last
 * array element.
 *
 * ex: textFormatArray(array('red', 'green', 'blue), ',', '&')
 *
 * Would return "red, green & blue"
 *
 * @param array $arr Array containing elements to be separated
 * @param string $separator What to insert between elements, except for last one
 * @param string $lastSeparator What to insert between second to last, and last element. Also, if there are only
 *             2 elements present, use this.
 * @param boolean $space Whether to insert a space after the separators
 */
function textFormatArray($arr, $separator, $lastSeparator, $space = TRUE)
{
    $combined = '';

    if ($space) {
        $space = ' ';
    } else {
        $space = '';
    }

    if (!is_array($arr)) {
        return $arr;
    }

    if (count($arr) == 1) {
        return array_shift($arr);
    }
    elseif (count($arr) == 2) {
        // If there are exactly 2 elements in array, return using $lastSeparator
        return implode($lastSeparator . $space, $arr);
    } else {
        // If 3 or more elements
        $combined.= array_shift($arr);
        foreach ($arr as $element) {
            // If we're not on last element
            if ($element !== end($arr)) {
                $combined.= $separator . $space . $element;
            } else {
                // If this is the last element, use different separator
                $combined.= $space . $lastSeparator . $space . $element;
            }
        }
        return $combined;
    }
}

This is more than what you asked, but it can work for more than simply commas.

Umm, Try using the implode function to join array elements.

for example

$zip = array('11111','22222','33333');
$fields = implode(",", $zip); // output: 11111,22222,33333

//SELECT * FROM customers WHERE zip IN ($fields)

go here for more examples http://php.net/manual/en/function.implode.php

Umm, Try using the implode function to join array elements.

for example

$zip = array('11111','22222','33333');
$fields = implode(",", $zip); // output: 11111,22222,33333

//SELECT * FROM customers WHERE zip IN ($fields)

go here for more examples http://php.net/manual/en/function.implode.php

I tried this and it just outputs the zips on one line with no comma between the zips.

foreach ($zips as $key => $value) {
	   $zip = array($key);
	$fields = implode(",", $zip);

Output example: 111112222233333

I tried this and it just outputs the zips on one line with no comma between the zips.

foreach ($zips as $key => $value) {
	   $zip = array($key);
	$fields = implode(",", $zip);

Output example: 111112222233333

$zip = implode(",", $zip);

or:

$zipArr = array();
foreach ($zips as $key => $value) {
    $zipArr[]= $key;
}
$zips = implode(", ", $zipArr);

or:

$zips = implode(", ", array_keys($zips));
Member Avatar for diafol
$in_string = implode(",",$zips);

//EDIT
jtreminio - sim. post. I think the array is $zips though not $zip, if he's using: foreach ($zips as $key => $value).

$zip = implode(",", $zip);

or:

$zipArr = array();
foreach ($zips as $key => $value) {
    $zipArr[]= $key;
}
$zips = implode(", ", $zipArr);

This only returns one zip code and not all of them.

edit: I think I got it. There is just a minor error as one zip code is showing up twice at the end.

edit2: I got the duplicate out of there! Thanks for your help!

please mark the thread as solved if you've managed to fix it

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.