implode(): Join elements of array to get 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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





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