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

ParameterMeaning
separatorSeparator to use to connect the array elements
piecesArray 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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions