PHP implode Code Example

Last Updated On Friday 17th Jun 2022

Definition

The implode() function returns a string from the elements of an array. In PHP, the function of implode is to convert the value of a one-dimensional array into a string.

Syntax

	implode(separator, array)
	

Parameters

  • separator – Optional. Specifies what to put between the array elements. Default is "" (an empty string).
  • array – Required. The array to join to a string

php implode

  • The order of the elements in the resultant string is the same as the order they appear in the array.
  • If the array is empty, then the implode() function will also return an empty string.

Examples

	$arr = ['Alice', 'Karl', 'Leo'];
echo implode(' | ', $arr);
echo implode('', $arr);
echo implode($arr);
	

Output

	Alice | Karl | Leo
AliceKarlLeo
AliceKarlLeo
	

With Associative array

If we use an associative array to the implode() function, then only the array’s values will be joined means the keys of the array will be ignored.

	$myDetails = [
    'name' => 'CodeJagd',
    'website' => 'codejagd.com',
    'madeWith' => 'SvelteKit'
];

echo implode(',', $myDetails);
	

Output

	CodeJagd,codejagd.com,SvelteKit
	
php implode
implode php
implode in php
implode python
php implode example
echo implode