Programming

Using Implode to convert Array to String

The PHP builtin function implode(), is used to join the elements of an array with a separator string.

The syntax of implode:

Parameters
separator: It is an optional parameter and its default value is an empty string.
array: It is an array of strings to implode

Examples of implode are:

  1. An Array to string 

Output

2. An Associative array to string

It is complex to join the key and value of an associative array using only implode function. Combining array_walk() and implode() functions of the PHP will make the job easier.

The array_walk() function walks through each element of an array in a user-defined callback function regardless of pointer position.

Parameters
array: The input array.
callback: Typically, a callback takes on two parameters. The array parameter’s value is the first, and the key/index second.
arg: arg is an optional parameter. If the callback function requires more than 2 parameters, the arg parameter is supplied. It is passed as the third parameter to the callback.

Output

3. Multidimensional array to string

Output

 

 

Hits: 4

Standard