PHP implode() Function

What is PHP implode() Function?

Joining all the elements of an array, If you want to create a string, use PHP implode() function. Practically, the function joins array elements with an empty string. It joins the elements in the same order as they are in the array.

You can remember the implode() function as “Array to String”.

Syntax:

implode(separator, array)

Parameters:

The Function has one required parameter and one optional parameter-

separator (Optional): it specifies how the function joins array elements to each other.

  • Though, it is an optional parameter, PHP recommends to use it for backward compatibility. The default value is “” (empty string). So, if you don’t mention this parameter, the function uses an empty string in place of it as the separator. And the string contains all the array elements one after another without any space. In other word, if you use empty string “” as the separator, the function will produce the same result. Check example 1.
  • You can use any character as a separator. Commonly used separators are – space (“ ”), comma (“,”), dash (“-”) etc. Choose a separator that meets your requirements.  Even you can use multiple characters as a separator. Check example 2.

array (Required): It is an array whose elements the function will use to join and create the resultant string.

Return Values:

The function returns a string that contains all the array elements that are joined by the separator.

Examples:

Example 1: Joining array elements with empty separator and no separator-

<pre>
<?php
$arr = array("Schools", "Of", "Web", ".com");
echo implode("", $arr);
echo "<br />";
$arr = array("Schools", "Of", "Web", ".com");
var_dump(implode($arr));
?>
</pre>

Output:

SchoolsOfWeb.com
SchoolsOfWeb.com

Explanation:

When you try to join all the elements with an empty separator, there remains no space between elements in the resultant string. The same result you can get when you don’t use the separator parameter. In this case, the function uses the empty string itself as the separator.

Example 2: Joining array elements with one character or multi-character separator-

<pre>
<?php
$arr = array("PHP", "MySQL", "HTML5", "CSS3");
echo implode(" ", $arr) . "<br />";
echo implode(", ", $arr) . "<br />";
echo implode(" - ", $arr) . "<br />";
echo implode(" | ", $arr) . "<br />";
echo implode("\t\t", $arr);
?>
</pre>

Output:

PHP MySQL HTML5 CSS3
PHP, MySQL, HTML5, CSS3
PHP – MySQL – HTML5 – CSS3
PHP | MySQL | HTML5 | CSS3
PHP                      MySQL                HTML5                CSS3

Explanation:

You can use different characters as separators. Check line 4 to line 8. You can use multi-characters as a separator too. See line 9.

Example 3: implode() function increase readability of an array to convert it to string-

<pre>
<?php
$arr = array("PHP", "Wordpress", "React", "Vue");
echo "Skills: ";
print_r($arr) . "<br />";
echo "Skills: " . implode(", ", $arr);
?>
</pre>

Output:

Skills: Array
(
    [0] => PHP
    [1] => WordPress
    [2] => React
    [3] => Vue
)

Skills: PHP, WordPress, React, Vue

Explanation:

implode() increases readability. The string that the function displays at line 6 has much more readability than the string that generates by the line 5.

Example 4: implode() can operator on binary values-

<pre>
<?php
$arr = array("PHP", "MySQL", "HTML5", "CSS3");
echo implode("\x2C ", $arr) . "<br />";
?>
</pre>

Output:

PHP, MySQL, HTML5, CSS3

Explanation:

“\x2C” is a hexadecimal value of character comma (,). The function use this as separator to join the array elements.

Example 5: implode() can also operators on Boolean values-

<pre>
<?php
$arr = array(TRUE, "TRUE", FALSE, TRUE);
echo implode("-", $arr);
?>
</pre>

Output:

string(9) “1-TRUE–1”

Explanation:

The first, third, and fourth elements are Boolean values. Implode() can join these values and create a string.

Example 6: implode() can’t join elements of an multi-dimensional array-

<pre>
<?php
$arr = array(
                array('PHP', 'Python'), 
                array('MySQL', 'MongoDB')
            );
echo implode(", ", $arr);
?>
</pre>

Output:

Warning:  Array to string conversion in D:\xampp\htdocs\php\implode.php on line 52
Warning:  Array to string conversion in D:\xampp\htdocs\php\implode.php on line 52
Array, Array

Explanation:

implode() function can’t join elements of a multi-dimensional array. It displays warning.

Practical Usages of PHP implode() Function:

This function has lots of practical usages. Few includes-

  • You can’t insert an array into a database table directly. Instead, you can convert the array into a string by this function and insert this into database table as a string. Later, when you need this data, you can convert this string back to an array using other built-in functions like explode(). Look at the sample code below-
    <?php
    $arr = array("PHP", "MySQL", "HTML5", "CSS3");
    $string = "INSERT INTO table('column') VALUES($arr)";
    ?>
    
  • As implode() function can create comma separated string. It is useful to create rows for a CSV file.
    <?php
    $arr = array("PHP", "20.87%", "1993");
    $row = implode(",", $arr);
    ?>
    
  • Sometimes it becomes easier to verify data from a string than an array. In this case, you can use implode() function to convert an array to a string.
  • For presentation purpose, a string is easier to read than an array. implode() can be used in this instance. Check example 3.

Notes on PHP implode() Function:

  • After performing implode() operation on an array, the array is never changed. It remains as it was before the operation. The function just creates a new string from the array.
  • It is a binary safe function. So, this can operate on binary data if it found one in its parameter. Check example 4.
  • The function not only operates on array that only contains string or numbers. You can convert an array to string that contains only Boolean values. In the resulting string, the function will convert Boolean TRUE to 1. Check example 5.
  • The explode() function only works on flat array or one-dimensional array, it doesn’t work on multi-dimensional array or nested array. Check example 6.
  • The function produce the same result whether it is an indexed array or associative array.
  • If the array has no element, the function returns an empty string.
  • PHP implode() function works same as the join() function.

Caution:

While implode() is a great way to convert an array to a string, but in case of a large array, the conversion may impact the performance. In such case, look for an alternate solution that better match the requirement.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP implode() Function

implode() is one of the frequently used built-in string functions in PHP. It is very popular way to convert an array to string.

Reference:

https://www.php.net/manual/en/function.implode.php