php tutorial - PHP implode method for converting arrays to string - php programming - learn php - php code - php script



The implode method in PHP

The implode of the method of PHP is used to convert an array into a string.

The resultant string may be joined with a specified character like comma, + sign etc.

Syntax of using the PHP implode method

Following is the general syntax of using the implode method:

var_string = implode ($glue,$string_arr);
click below button to copy the code. php tutorial - team

The implode function may accept two parameters:

  • The $glue represents a character or symbol that is used to join all array elements.
  • The $string_arr is the array that you want to convert into a string.
  • 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 implode with comma as $glue

In this demo of implode function, an array of string with three elements is created. After that, the implode function is used where 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.

implode

Learn php - php tutorial - implode - php examples - php programs

The following code is used in the above example:

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

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

echo "The returned string after implode with comma: <br><strong>".$str_Names ."</strong>";
click below button to copy the code. php tutorial - team

Using space as a separator in implode function

You may use some other character or symbol as a separator in the implode function of PHP. In this example, rather than using a comma, I am using a space as a separator. See the output and code online:

implode-space

Learn php - php tutorial - implode-space - php examples - php programs

This is how the array is created and implode function is used in 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>";
click below button to copy the code. php tutorial - team

A demo of using implode with HTML list

You may use implode with markup as well to build HTML list. In this example, an array of courses is created. After that, implode function is used where $glue is <li> tags. See the demo online:

implode-html

Learn php - php tutorial - implode-html - php examples - php programs

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

<?php
$courses = array('PHP', 'MySQL', 'jQuery','JavaScript','Bootstrap');
echo "<ul><li>" . implode("</li><li>", $courses) . "</li></ul>";
?>
click below button to copy the code. php tutorial - team

Similarly, the array can be created by using the DB driven values and you may create list or use other markup tags to create a section of 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 by the separator to convert this 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 implode function is used to convert an array into a string.

implode-associative-array

Learn php - php tutorial - implode-associative-array - php examples - php programs

This is how the implode function is used with an associative array:

$arr_courses = array(
"course1" => "PHP",
"course2" => "MySQL",
"course3" => "Java",
);
echo implode(",", $arr_courses);
click below button to copy the code. php tutorial - team

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

The order of using parameters in implode function

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

var str = implode(",", $arr_name);
click below button to copy the code. php tutorial - team

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

var str = implode($arr_name ,",");
click below button to copy the code. php tutorial - team

See an example below with this format:

php-implode-format

Learn php - php tutorial - php-implode-format - php examples - php programs

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

However, 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.

See this demo online by clicking the link or image below:

php-implode-two-dimensional-array

Learn php - php tutorial - php-implode-two-dimensional-array - php examples - php programs

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'

)

);
click below button to copy the code. php tutorial - team

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

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

return $entry['Name'];

}, $two_arr));
click below button to copy the code. php tutorial - team

The output of the example is names separated by commas.


Related Searches to PHP implode method for converting arrays to string