Latest Code Tutorials

PHP implode: How to Join Array Elements

The implode() function returns the string from the elements of the array. The function is binary-safe. We join the array elements with the string. Like the join() function, the implode() function also returns the string formed from the elements of the array.

PHP implode

PHP implode() is a built-in function used to join the elements of an array and return the string. To convert an array to string in PHP, use the implode() function. If we have an array of elements, we can use the implode() function to join them all to form one string.


See the syntax of PHP implode() function.



The separator parameter is optional and specifies what to put between the array elements.

The array parameter is required, and it is the array to join the string.


See the following example.


// app.php

$frontend = ['Svelete','Angular','React', 'Vue'];
echo implode("|", $frontend);

See the below output.

PHP Implode Example

The implode() function accepts its parameters in either order. However, for consistency with the explode() function, you should use the documented order of arguments.

You can note that an array with one or no elements works fine.

For example, see the code.


// app.php

$arrA = ["Asylum","House","Coven"];
$arrB = ["AHS"];
$arrC = [];

echo implode("|", $arrA)."\n";
echo implode("|", $arrB)."\n";
echo implode("|", $arrC);

See the below output.

PHP implode() Function Tutorial

The implode() function in PHP is easily remembered as “array to a string,” which means it takes an array and returns a string. Then, it rejoins any array elements and returns the resulting string, put in a variable.

PHP implode() function on Associative Arrays

The implode function acts on array “values,” disregarding any keys. See the following example.


// app.php

$devs = ['1st' => 'CloudArchitecht',
        '2nd' => 'DevOps',
        '3rd' => 'DataScientists'];

echo implode(" | ", $devs)."\n";

See the below output.

PHP implode() function on Associative Arrays

The implode() function also be used for building tags or complex lists, like the following.


$elements = array('a', 'b', 'c');

echo "<ul><li>" . implode("</li><li>", $elements) . "</li></ul>";

Also, it is quite handy in INSERT statements.


 // array containing data
 $array = array(
  "name" => "Krunal",
  "surname" => "Lathiya",
  "email" => ""

// build query...
$sql  = "INSERT INTO table";

// implode keys of $array...
$sql .= " (`".implode("`, `", array_keys($array))."`)";

// implode values of $array...
$sql .= " VALUES ('".implode("', '", $array)."') ";

// execute query...
$result = mysql_query($sql) or die(mysql_error());

If you want to implode the array of booleans, you will get a strange result. See the following code.


// app.php

var_dump(implode('', array(false, true, false, false, true)));

It is worth noting that if you call implode on the string rather than the array, you do not get your string back; you get NULL.

The implode() function returns a string

The null values are imploded too. So you can use an array_filter() to sort out the null values.


$ar = array("krunal", null, "lathiya");

print(implode(',', array_filter($ar, function($v){ return $v !== null; })));

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.