PHP add single quotes to comma separated list

PHP add single quotes to comma separated list

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP add single quotes to comma separated list

When you implode an array in PHP, the result is typically a plain string of values separated by delimiters. If your input looks like qwerty, QTPQ, FRQO, it often comes from a source that already treats these as raw strings. Adding single quotes around each element—turning it into 'qwerty', 'QTPQ', 'FRQO'—is a common requirement when preparing data for SQL queries or formatted output. While the goal seems simple, achieving the exact spacing and punctuation requires a bit more than just wrapping the string in characters.

The most straightforward approach uses array_map() combined with implode(). You iterate through the array, wrap each element in quotes, and then join them back together with commas. Here is how you would implement it:

$data = ['qwerty', 'QTPQ', 'FRQO'];

// Wrap each item in single quotes
$quoted = array_map(function ($value) {
    return "'$value'";
}, $data);

// Join with a comma and space
$result = implode(', ', $quoted);

echo $result; // Outputs: 'qwerty', 'QTPQ', 'FRQO'

This method is explicit and easy to read. It guarantees that every element gets its own pair of quotes, regardless of the original array contents. If your input contains single quotes already—for example, 'It''s'—you will need to handle escaping to avoid syntax errors in downstream SQL contexts. A robust implementation would look like this:

function quoteValues($values) {
    $escaped = array_map(function ($value) {
        // Escape existing single quotes by doubling them for SQL safety
        $safeValue = str_replace("'", "''", $value);
        return "'$safeValue'";
    }, $values);
    
    return implode(', ', $escaped);
}

list($id, $name, $status) = quoteValues(['1', "O'Brien", 'active']);

In this version, str_replace() doubles any single quotes found within the data before wrapping them. This follows standard SQL escaping conventions where a single quote is represented as two single quotes inside a string literal. Without this step, a value like O'Brien would break the query syntax if passed directly to a database driver.

Another option is using preg_replace() to inject quotes and commas in one pass, but that adds unnecessary complexity for simple arrays. Regular expressions shine when dealing with large blocks of text or CSV parsing, not when you have direct access to an array structure. Stick with the functional approach unless performance profiling proves otherwise.

If you are working within a Laravel application, the same logic applies. You can create a helper method in your App namespace or use Eloquent’s raw query capabilities carefully. Remember that binding parameters via $query->where('name', $name) handles quoting and escaping automatically. Manually constructing quoted strings is only necessary when generating dynamic fragments for logging, debugging, or non-parameterized contexts.

One potential pitfall is assuming the input array contains only simple strings. If your data includes null values, arrays within arrays, or objects that cast to strings unexpectedly, the output might not match expectations. Always validate or sanitize inputs before applying transformations. Also, be mindful of trailing commas if you later decide to append a new item dynamically; the implode() function will handle spacing correctly, but logic outside of it must remain strict about count and order.

For production environments, consider caching the transformed string if the underlying data doesn't change frequently. This avoids recomputing the quoted list on every request, saving CPU cycles in high-traffic scenarios.

Our network