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:
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.
Learn php - php tutorial - implode - php examples - php programs
The following code is used in the above example:
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:
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:
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:
Learn php - php tutorial - implode-html - php examples - php programs
This is how the list is build by using the implode function:
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.
Learn php - php tutorial - implode-associative-array - php examples - php programs
This is how the implode function is used with an 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 implode function
You may use any order of specifying the parameters in the implode function of PHP. In above example, I used this format:
This order will also produce the same result without any error:
See an example below with this 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:
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:
After that, the implode method is used to get the values of “Name” elements. These are joined by using the “,” separator:
The output of the example is names separated by commas.