by Hiroshi on June 23rd, 2008

The PHP function implode operates on an array and is known as the “undo” function of explode. If you have used explode to break up a string into pieces or parts or just have an array of stuff you can use implode to put them all into one string.

PHP Implode – Combining Exploded Pieces

The first argument of implode is the string of characters you want to use to join the array pieces together. The second argument is the array (pieces).

<?php
$pieces = array("Hello", "World,", "I", "am", "Here!");
 
$WithSpaces = implode(" ", $pieces);
$WithDashes = implode("-", $pieces);
 
for($i = 0; $i < count($pieces); $i++)
{
	echo "Piece no $i = $pieces[$i] <br />";
}
 
echo "<br />";
 
echo "Combined With Spaces = $WithSpaces <br />";
echo "Combined With Dashes = $WithDashes";
?>

Result

Piece no 0 = Hello
Piece no 1 = World,
Piece no 2 = I
Piece no 3 = am
Piece no 4 = Here!
 
Combined With Spaces = Hello World, I am Here!
Combined With Dashes = Hello-World,-I-am-Here!
  • Share/Bookmark

,

Leave a Review