How to Join an Array into a String in PHP with implode
implode(', ', $array) in PHP joins an array's values into one string, with ', ' between each pair. It turns numbers into text and returns an empty string for an empty array, but a value that is itself an array comes out as the word Array, with a warning.
Syntax
$<text> = implode('<separator>', <array>);
| Part | What it does |
|---|---|
'<separator>' | The text to put between values, such as ', '. |
<array> | The array whose values to join. |
$<text> | The joined string. |
The PHP manual: implode "Returns a string containing a string representation of all the array elements in the same order, with the separator string between each element."
The Example
The example joins a list of two roles twice, once with a comma and once with a bar. Then it joins a list with only one value, a list of three numbers and an empty array. Finally, one of the roles turns out to be an array itself.
Prerequisites
- PHP. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
declare(strict_types=1);
$roles = ['customer', 'beta-tester'];
echo implode(', ', $roles) . "\n";
echo implode(' | ', $roles) . "\n";
echo implode(', ', ['only one']) . "\n";
echo implode(', ', [10, 20, 30]) . "\n"; // numbers are turned into text
var_dump(implode(', ', [])); // an empty array gives an empty string
var_dump prints a value with its type and length, which shows an empty string that echo would print as nothing.
Step 2: Run It
php main.php
customer, beta-tester
customer | beta-tester
only one
10, 20, 30
string(0) ""
The command is the same in zsh, bash and PowerShell.
Step 3: Put an Array Inside the Array
Replace main.php with a version whose second role is an array:
<?php
declare(strict_types=1);
// The second item is itself an array.
$roles = ['customer', ['admin', 'support']];
echo implode(', ', $roles) . "\n";
php main.php
PHP Warning: Array to string conversion in /Users/Shared/you/main.php on line 8
Warning: Array to string conversion in /Users/Shared/you/main.php on line 8
customer, Array

What Happened
- The separator went only between values, never before the first or after the last, so a list with one value gave just that value.
- The numbers became text:
10, 20, 30. - The empty array gave an empty string, which
var_dumpshowed asstring(0) "". - The inner array became the word
Array, with the warningArray to string conversion.implodeturns each value into a string, and that is what an array becomes as a string. - The warning appears twice because this PHP's settings both log errors to the terminal, as
PHP Warning:, and display them, asWarning:.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing the array first, as in implode($roles, ', ') | TypeError: implode(): Argument #1 ($separator) must be of type string, array given | Put the separator first |
| Joining an array that holds arrays | Warning: Array to string conversion, and the word Array in the text | Join each inner array first |
| Expecting the keys of an associative array in the text | implode joins only the values | Join array_keys($array) to get the keys |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: implode(): Argument #1 ($separator) must be of type string, array given | The arguments are the wrong way round | Pass the separator first and the array second |
Warning: Array to string conversion | One of the values is an array | Join the inner array first, or leave it out |
FAQ
Is join() different from implode()?
No. The PHP manual describes join as an alias of implode, and in my check join(', ', $roles) printed customer, beta-tester too.
Can I call implode with only the array?
Yes. The separator is then an empty string: implode($roles) printed customerbeta-tester.
What do true, false and null become?
true becomes 1, and false and null become empty strings: implode(',', [true, false, null, 1.5]) printed 1,,,1.5.
What happens to the keys of an associative array?
They're left out. implode(', ', ['first' => 'customer', 'second' => 'beta-tester']) printed customer, beta-tester, and implode(', ', array_keys(...)) of the same array printed first, second.
Conclusion
implode joins an array's values into one string with a separator between each pair. Put the separator first, and make sure no value is itself an array.
Additional Resources
- implode and join, in the PHP manual.
- array_keys, in the PHP manual.
