Skip to main content

How to Join an Array into a String in PHP with implode

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
$<text> = implode('<separator>', <array>);
PartWhat 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:

main.php, which joins the roles customer and beta-tester with a comma and with a bar, then joins a one-value list, the numbers 10, 20 and 30, and an empty array, printing the last result with var_dump.

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​

Terminal
php main.php
Output
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:

main.php
<?php

declare(strict_types=1);

// The second item is itself an array.
$roles = ['customer', ['admin', 'support']];

echo implode(', ', $roles) . "\n";
Terminal
php main.php
Output
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

A zsh terminal where php main.php prints customer, beta-tester, then the roles joined with a bar, only one, 10, 20, 30 and string(0) with empty quotes; for the changed file it prints PHP Warning: Array to string conversion twice, then 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_dump showed as string(0) "".
  • The inner array became the word Array, with the warning Array to string conversion. implode turns 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, as Warning:.

Common Mistakes​

MistakeWhat happensDo this instead
Passing the array first, as in implode($roles, ', ')TypeError: implode(): Argument #1 ($separator) must be of type string, array givenPut the separator first
Joining an array that holds arraysWarning: Array to string conversion, and the word Array in the textJoin each inner array first
Expecting the keys of an associative array in the textimplode joins only the valuesJoin array_keys($array) to get the keys

Troubleshooting​

SymptomLikely causeFix
TypeError: implode(): Argument #1 ($separator) must be of type string, array givenThe arguments are the wrong way roundPass the separator first and the array second
Warning: Array to string conversionOne of the values is an arrayJoin 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​