Introduction to PHP implode
PHP implode function is one of the functions built-in used for joining elements of an array. This function works similarly to join() and is an alias of the same.
We use the implode function in an array of elements to join all of them to make a string. Array elements are usually joined by the help of a string and use a delimiter as specified by us. Hence implode function gives us the string resulting from the formation of elements of the array similar to the join() function.
Syntax
string implode(separator,array);
Parameter
Two parameters are required as input to this function where one is optional and the other is mandatory one.
- separator: This is a non-mandatory input component of type string. The array values here will be joined to make a string and will be distinguished with the help of a separator parameter given here. When it is not given default value is taken which is an empty string (“”).
- array: This is a mandatory field that specifies the array that has to be joined to create the string.
Return type: This implode() function returns a string as its output. From the array elements, it will return the string which is newly joined.
Examples of PHP implode
Below are some of the examples based on implode function which covers a few possible scenarios where they are or can be implemented:
Example #1
Code:
<?php
// Below is PHP Code representing implementation of implode function
$Input = array('first','string','combination');
// Using function without separator parameter
print_r(implode($Input));
print_r("\n");
// Using function with separator
print_r(implode("-",$Input));
?>
Output:
In this basic example, we are showing how to use implode function both with and without using the non-mandatory separator parameter. So in the first line of output, it prints the array by joining all 3 strings mentioned in the input as it is. In the second line of output, we have the array output as a combination of the input strings but along with it and the separator parameter is also printed in between two words.
Example #2
Code:
<?php
$arr = array('string1', 'string2', 'string3');
//Using implode function to make the strings as comma seperated
$sep= implode(",", $arr);
echo $sep;
print_r("\n");
// When an empty array is used returns an empty string
var_dump(implode('check', array())); // string(0) ""
?>
Output:
In this example, we are first declaring 3 strings as part of an array “arr”. Next, we are using implode function and mentioning the comma separator to use it for separating these 3 strings. We are also displaying what happens when an empty array is used. It returns an empty string in this case as shown.
Example #3
Code:
<?php
//Declaring 3 different array lists
$arr1 = array("1","2","3");
$arr2 = array("one");
$arr3 = array();
echo "array1 is: '".implode("'/'",$arr1);
print_r("\n");
echo "array2 is: '".implode("'-'",$arr2);
print_r("\n");
echo "array3 is: '".implode("','",$arr3);
?>
Output:
In this example, we will be showing three different cases of arrays. The first line of output displays when the array is having 2 or more strings in its array elements and we are joining these using implode function and / as the delimiter. The second line of output displays when the array is having a single element and we are using “-” delimiter for the same. The third line of output shows what happens when there is an empty array. The output will be printed as is without any errors.
Example #4
Code:
<?php
$arr1 = array('One', 'Two', 'Three');
echo "<ol><li>" . implode("</li><li>", $arr1) . "</li></ol>";
?>
Output:
In this example, we are displaying how to implode function can also be used in the case of HTML tags by using tags as the separators. Here we are making using the array to display its elements in the form of ordered lists.
Example #5
Code:
<?php
declare(strict_types=1);
$arr1 = array( 'str1','str2','str3' );
$arr2 = array( '1st' => 'one', 'two', '2nd' => 'three' );
echo implode( '-', $arr1 ),'.', implode( '-', $arr2 );
?>
Output:
In this example, we can see that the implode function acts upon only the values of array elements and completely disregards its keys. Here ‘str1′,’str2′,’str3’ are the values directly declared in arr1 whereas in arr2 the keys are “1st”, “2nd” and their respective value pairs are “one”,”two” and “three”.
Example #6
Code:
<?php
class Test
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function __toString()
{
return $this->name;
}
}
$arr = [
new Test('one'),
new Test('two'),
new Test('three')
];
echo implode('; ', $arr);
?>
Output:
In the above example, we can see that even objects can be used alongside the implode function but the only condition for this is that the objects should apply the toString() function as shown.
Example #7
Code:
<?php
var_dump(implode('',array(true, false, false, true, true)));
?>
Output:
In this example, we are displaying what happens when a boolean set of values are used as array elements. It results in a different kind of output where we get the output in the form of 1’s wherever true is present and in place of false, it outputs null i.e empty value.
Conclusion
PHP implode() function as shown in the above examples can be used in various cases where there is a need to join different elements of an input array. It is a simple function having only 2 parameters where we specify the delimiter to be used to separate the array elements.
Recommended Articles
This is a guide to PHP implode. Here we discuss the introduction to implode function along with the syntax, parameters, and respective examples. You can also go through our other suggested articles to learn more –
5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses