implode() is a built-in function of PHP that generates a string value by combining the elements of an array with a delimiter. It works like another built-in function of PHP, join(). This function is used for various purposes in the script. For example, when we need to pass the string data with space from one script to another, then the string data can be converted into an array using another built-in function, explode(), before passing the data into another script. The original string data can be retrieved from the array in the second script using the implode() function. How this function can be used in a PHP script is shown in this tutorial.
Syntax:
implode() function can be used in two ways in the script. It can be used with or without a delimiter. The syntaxes of this function are given below:
string implode (array $array)
When the implode() function is used without any delimiter, it returns a string value by combining all elements of the $array.
string implode (string $glue, array $array)
When the implode() function is used with any particular delimiter, it returns a string value by adding the delimiter with each element of the $array.
Example 1: Use of implode() function without delimiter
The following script shows the way to use the implode() function without any delimiter to combine the array values. Create a PHP file with the following script.
An array variable named $color is defined with five string values. The implode() function will return a string by combining the array values with empty string that will print later.
Output:
The following output will appear after running the script from the webserver. It shows the six values of the array as a string.
Example 2: Use of implode() function with space delimiter
The following script shows the way to use the implode() function with space(‘ ’) delimiter. An array of numeric values is defined in the script. The script will return a string by combining the array values with space.
Output:
The following output will appear after running the script from the webserver. The six number values of the array are separated by space in the output.
Example 3: Use of implode() function with comma(,) delimiter
In the previous two examples, no value of the array contains any space. But if the value contains the string of multiple words, then the space delimiter can be used to separate the array values. The following script shows the use of the implode() function with a comma(, ) delimiter. Create a PHP file with the following script.
An array named $names is defined in the script with five string values of multiple words. Next, implode() function is used with comma(,) delimiter to join the values of $names. The returned value of this function is stored in the variable $str that is printed later.
//Define an array of strings with multiple words
$names = array('Nikhil Proctor', 'Kaylee Potter', 'Cloe Whittaker', 'Laila Murphy', 'Rochelle Palmer');
//implode() function with space delimiter
$str = implode(', ', $names);
echo "<h3>The output of implode() function with comma(,) delimiter:</h3> <b style='color:red'> $str </b><br />";
?>
Output:
The following output will appear after running the script from the webserver. The values of the array are printed with the comma (,) separator in the output.
Example 4: Use of implode() function in two-dimensional array
A one-dimensional array is used in the previous three examples. The following script shows how the implode() function can be used to join the values of a two-dimensional array. A two-dimensional array named $assoc_arr is declared in the script that contains four values. ‘<br/>’ is used as the delimiter in the script to create a new line after each array value when joining the array values using the implode() function.
//Define an associative array
$assoc_arr = array ('01' => 'google.com', '02' => 'ask.com', '03' => 'bing.com', '04' => 'yahoo.com');
echo "<h3>The list of search engine sites are: </h3>";
//Print the implode() function with <br /> as delimiter
echo "<b style='color:purple'>". implode('<br />', $assoc_arr). "</b>";
?>
Output:
The following output will appear after running the script from the webserver. The values of the array are printed line by line in the output.
Example 5: Use of implode() function with a word delimiter
In the previous examples, a single character and a <br/> tag are used as a delimiter to join the array values. The following example shows the use of the word as a delimiter in the implode() function. Create a PHP file with the following script.
An array of the six string values is defined in the script. ‘ and ’ is used as delimiter of implode() function in the script. The array values will be printed by adding the word delimiter between the values of the array.
//Define an array of strings
$flowers = array('Rose', 'Lity', 'Tulip', 'Water Lily', 'Orchid', 'Daisy');
//implode() function with 'and ' delimiter
$str = implode(' and ', $flowers);
echo "<h3>The output of implode() function with ' and ' delimiter:</h3> <b style='color:pink;font-size:20px'> $str </b><br />";
?>
Output:
The following output will appear after running the script from the webserver. It shows the array values as a string by adding ‘and ‘ as a separator of the array values.
Conclusion
The use of the implode() function is explained in this tutorial using different types of delimiters. This tutorial will help the readers know the ways of using the implode() function in PHP script and apply this function in their script properly.