PHP implode [Array to String Conversion]

Illustration of PHP implode function: Strings merging into one.

PHP implode

The implode function of PHP is used to convert an array into a string. The resultant string may be joined with a specified character like a comma, + sign, etc.

Syntax of using the PHP implode method

Following is the general syntax for using the implode method:

var_string = implode ($glue,$string_arr);

The implode function may accept two parameters:

Parameter Description
$glue The $glue represents a character or symbol that is used to join all array elements.
$string_arr The $string_arr is the array that you want to convert into a string.
Return value The return value after using the implode PHP function is a string, so you may assign it to a string variable.

 

See the following examples for using the implode function with online demos and code.

An example of PHP implode with comma as $glue

  • In this demo of implode function, an array of strings with three elements is created.
  • After that, the implode function is used where a comma is given as a separator or glue character.
  • The resultant string is assigned to a string variable which is displayed by using the echo statement of PHP.

Complete code with markup:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #408080;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
  
</head>
<body>
<div class="divcls"><h3>A demo of PHP implode function</h3>
< ?php 

$arr_Names = array("Tina","Mena","Nina");

$str_Names=implode(",", $arr_Names);

echo "The returned string after implode with comma: <br><strong>".$str_Names ."</strong>";
?>
</div>

 
</body>
</html>

Output:

PHP implode

Using space as a separator in the implode function

You may use some other character or symbol as a separator in the implode function. In this example, rather than using a comma, I am using a space as a separator.

See the output and code below:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #008040;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>A demo of PHP implode function</h3>
< ?php 

$arr_implode = array("This","is","an", "implode", "tutorial!");

$str_arr=implode(" ", $arr_implode);

echo "Returned string after implode with space: <br><br><strong>".$str_arr ."</strong>";
?>
</div>

 
</body>
</html>

Output:

PHP implode spaceThis is how the array is created and the implode function is used in the above example:

$arr_implode = array("This","is","an", "implode", "tutorial!");

$str_arr=implode(" ", $arr_implode);



echo "Returned string after implode with space: <br><br><strong>".$str_arr ."</strong>";

A demo of using implode with an HTML list

You may use PHP implode with markup as well to build an HTML list.

In this example, an array of courses is created. After that, the implode function is used where $glue is <li> tags.

See the demo code that you can use:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #008040;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>A demo of PHP implode function</h3>
< ?php 

$courses = array('PHP', 'MySQL', 'jQuery','JavaScript','Bootstrap');

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

?>
</div>

 
</body>
</html>

Output:

PHP implode HTML

This is how the list is built by using the implode function:

<?php
$courses = array('PHP', 'MySQL', 'jQuery','JavaScript','Bootstrap');

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

Similarly, the array can be created by using the DB-driven values and you may create a list or use other markup tags to create a section of the web page by using the implode function.

Using implode function with associative arrays

In simple use, there is no difference in using the implode function with numeric or associative arrays.

The implode function will take the array elements and join them with the separator to convert them into a string. The sequence will remain the same as it was created in the array.

See an example below where an associative array is created and the implode function is used to convert an array into a string.

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #008040;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Associative arrays with PHP implode</h3>
< ?php 

$arr_courses = array(

"course1" => "PHP",

"course2" => "MySQL",

"course3" => "Java",

);

echo implode(",", $arr_courses);


?>
</div>

 
</body>
</html>

Output:

PHP implode associative array

You see, there is no difference compared to above examples where numeric index-based arrays were used.

The order of using parameters in the implode function

You may use any order of specifying the parameters in the implode function. In above example, I used this format:

var str = implode(“,”, $arr_name);

This order will also produce the same result without any error:

var str = implode($arr_name ,”,”);

See an example below for this format:

<!doctype html>
<html>

<head>

<style>
.divcls {
  background: #008040;
  height: auto;
  width:300px;
  border-radius: 15px;
  padding:20px;
  font-size:16px;
  color:#fff;   

}
</style>
 

  
</head>
<body>
<div class="divcls"><h3>Associative arrays with PHP implode</h3>
< ?php 

$arr_courses = array(

"course1" => "PHP",

"course2" => "MySQL",

"course3" => "Java",

);

echo implode($arr_courses,",");


?>
</div>

 
</body>
</html>

Output:

PHP implode format

You see, the output of the associative array in the above two examples is the same.

Note: The official PHP documentation recommends using the first way as this format is used in the explode method as well.

An example of using implode with two-dimensional array

You may use the implode method in multi-dimensional arrays in PHP as well.

In this demo, a two-dimensional array is created.

After that, the implode method is used to get the elements that are combined/joined by using a comma separator.

First of all, a two-dimensional array is created in the example:

$two_arr = array(

array(

'Name' => 'Mike',

'Email' => 'abc@gmail.com'

),

array(

'Name' => 'Shaun',

'Email' => 'def@gmail.com'

)

);

After that, the implode method is used to get the values of the “Name” elements. These are joined by using the “,” separator:

echo implode(', ', array_map(function ($entry) {

return $entry['Name'];

}, $two_arr));

Output:

Mike, Shaun

The output of the example is names separated by commas.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!