PHP String
$ar=array('Welcome','to','plus2net','php','section');
echo implode($ar);
The output is here
Welcometoplus2netphpsection
Sorry , we missed the space between words so let us add one gap between
$ar=array('Welcome','to','plus2net','php','section');
echo implode(" ",$ar);
The output is here
Welcome to plus2net php section
Example 1: Joining Array Elements with a Custom Separator
<?php
$ar = array('apple', 'banana', 'cherry');
echo implode(" - ", $ar); // Output: apple - banana - cherry
?>
Example 2: Imploding with Empty Strings
<?php
$ar = array('P', 'H', 'P');
echo implode("", $ar); // Output: PHP
?>
Example 3: Joining Elements from an Associative Array
<?php
$ar = array('name' => 'John', 'age' => 25);
echo implode(", ", $ar); // Output: John, 25
?>
Example 4: Imploding Multi-Dimensional Arrays
<?php
$multi_array = array(array('a', 'b'), array('c', 'd'));
foreach($multi_array as $arr) {
echo implode(", ", $arr) . "<br>"; // Output: a, b and c, d on new lines
}
?>
Example 5: Handling Empty Arrays with implode()
<?php
$empty_array = array();
echo implode(", ", $empty_array); // Output: (empty string)
?>
← String Functions explode(): Array from string →
← Subscribe to our YouTube Channel here