PHP Array to String by implode function

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 comma, + sign etc.

implode with simple array example With associative array

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:

  • 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 PHP 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 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.

PHP implode

See online demo and code

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. In this example, rather than using a comma, I am using a space as a separator. See the output and code online:

PHP implode space

See online demo and code

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 PHP 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:

PHP implode HTML

See online demo and code

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

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 by the separator to convert 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.

PHP implode associative array

See online demo and code

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. In above example, I used this format:

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

See an example below for this format:

PHP implode format

See online demo and code

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

See online demo and code

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.