PHP implode() Function
Definition
The implode()
function converts an array into a string by inserting a separator
between each element.
This is the reverse of the explode()
function.
Syntax
PHP implode() Function has the following syntax.
string implode ( string separator, array pieces )
Parameter
Parameter | Meaning |
---|---|
separator | Separator to use to connect the array elements |
pieces | Array to implode |
Example 1
Convert array to string
<?PHP// w w w .j a v a 2s .c om
$my = "A and B and C";
$my_array = explode(" and ", $my);
print_r($my_array);
print "\n";
$exclams = implode("! ", $my_array);
print_r($exclams);
print "\n";
$fruitArray = array( "apple", "pear", "banana", "strawberry", "peach" );
$fruitString = implode( ",", $fruitArray );
echo $fruitString;
?>
The code above generates the following result.