Home > Article > Backend Development > PHP implode
Implode is a built-in function in PHP that links array elements. This function works similarly to bind () and is an alias. In order to unite all of the components in an array to create a string, we utilize the implode function. Hence implode function gives us the string resulting from forming array elements similar to the join() function.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
string implode(separator,array);
Parameter
Return type: This implode() function returns a string as its output. From the array elements, it will return the newly joined series.
Below are some of the examples based on implode function,, which covers a few possible scenarios where they are or can be implemented:
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:
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 first declare 3 strings as part of an array “arr”. Next, we are using implode function and mentioning the comma separator to use for separating these 3 strings. We are also showing the results of using an empty array. It returns an empty string in this case, as shown.
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 has 2 or more strings in its array elements, and we are joining these using implode function and/or as the delimiter. The second line of output displays when the array has a single element, and we are using the “-” 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.
Code:
<?php $arr1 = array('One', 'Two', 'Three'); echo "<ol><li>" . implode("</li><li>", $arr1) . "</li></ol>"; ?>
Output:
Here we are making using the array to display its elements in the form of ordered lists.
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”.
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.
Code:
<?php var_dump(implode('',array(true, false, false, true, true))); ?>
Output:
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.
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 with only two parameters where we specify the delimiter to be used to divide the array components.
The above is the detailed content of PHP implode. For more information, please follow other related articles on the PHP Chinese website!