PHP array to comma separated string (code snippets included)

To convert a PHP array into a comma separated string, you need to use the implode() function.

For a multi dimension array, you need to use the array_column() function to grab the array values you want to convert to string.

This tutorial will help you convert:

Let’s begin.

PHP single dimension array to comma separated string

The implode() function is used to join array elements as a string.

The function syntax is as follow:

implode(string $separator, array $array): string

To create a comma separated string from an array, pass a comma (,) as the $separator argument into the implode() function.

Consider the example below:

<?php

// ๐Ÿ‘‡ create a PHP array
$animals = ["eagle", "leopard", "turtle"];

// ๐Ÿ‘‡ create a comma separated string from array
$array_string = implode(",", $animals);

// ๐Ÿ‘‡ print the string
print $array_string;
?>

The code above will produce the following output:

eagle,leopard,turtle

You can also add a space after the comma to make the string easier to read:

<?php

// ๐Ÿ‘‡ create a PHP array
$animals = ["eagle", "leopard", "turtle"];

// ๐Ÿ‘‡ create a comma separated string from array
$array_string = implode(", ", $animals);

// ๐Ÿ‘‡ print the string
print $array_string;
?>

The output:

eagle, leopard, turtle

PHP multi dimension array to comma separated string

The implode() function can’t handle a multi dimension array.

When you pass a multi dimension array, the function produces a warning and returns the wrong result.

Consider the following example:

<?php
// ๐Ÿ‘‡ create multi dimension array
$users = [
    [
        "id" => 2135,
        "first_name" => "John",
    ],
    [
        "id" => 3245,
        "first_name" => "Sally",
    ],
    [
        "id" => 5342,
        "first_name" => "Jane",
    ],
    [
        "id" => 5623,
        "first_name" => "Peter",
    ],
];

// ๐Ÿ‘‡ call implode
$array_string = implode(", ", $users);

// ๐Ÿ‘‡ print the string
print $array_string;

The code above will produce the following output:

Warning: Array to string conversion in ... on line ...
Array, Array, Array, Array

To create a comma separated string from a multi dimension array, you have to extract the column you want to process using the array_column() function first.

The syntax is as follows:

array_column(
    array $array,
    int|string|null $column_key, 
    int|string|null $index_key = null
): array

The array_column() function return the values of an array that has a specific $column_key.

Once you have a single array returned from array_column(), call the implode() function.

For example, suppose you want to create a comma separated string from the first_name data. Here’s how you do it:

<?php
// ๐Ÿ‘‡ create multi dimension array
$users = [
    [
        "id" => 2135,
        "first_name" => "John",
    ],
    [
        "id" => 3245,
        "first_name" => "Sally",
    ],
    [
        "id" => 5342,
        "first_name" => "Jane",
    ],
    [
        "id" => 5623,
        "first_name" => "Peter",
    ],
];

// ๐Ÿ‘‡ get the first_name data
$first_names = array_column($users, 'first_name');

// ๐Ÿ‘‡ call implode
$array_string = implode(", ", $first_names);

// ๐Ÿ‘‡ print the string
print $array_string;

The code above will have the following output:

John, Sally, Jane, Peter

You can also pass an int as the $column_key argument to extract the value at a specific index.

Here’s an example:

// ๐Ÿ‘‡ a multi dimension array without string keys
$animals = [
    ["eagle"], 
    ["leopard"], 
    ["turtle"]
];

// ๐Ÿ‘‡ get the animals data using index
$animal_names = array_column($animals, 0);

// ๐Ÿ‘‡ call implode
$array_string = implode(", ", $animal_names);

// ๐Ÿ‘‡ print the string
print $array_string;

The output:

eagle, leopard, turtle

Now you’ve learned how to convert a PHP array to a comma separated string.

You’ve also seen examples of converting a single dimension and multi dimension PHP array.

I hope this tutorial has been useful for you ๐Ÿ™

More PHP tutorials

See all