implode(): Join elements of array to get string

Join array values into a string

implode() joins array values using a separator. Use the current argument order: separator first, then the array.

$colors = ['red', 'green', 'blue'];
echo implode(', ', $colors); // red, green, blue

Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.

$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



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer