Home  >  Article  >  Backend Development  >  PHP implode

PHP implode

王林
王林Original
2024-08-29 13:05:38846browse

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

string implode(separator,array);

Parameter

  • Separator: For a type string, this input field is optional. Before the array values are concatenated to make a string, they will first be separated using the separator parameter that is provided above. If it is left out, the empty string (“) is used as the default value.
  • Array: The variety that needs to be linked to create the string is specified in this field, which is required.

Return type: This implode() function returns a string as its output. From the array elements, it will return the newly joined series.

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:

PHP implode

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:

PHP implode

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.

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:

PHP implode

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.

Example #4

Code:

<?php
$arr1 = array('One', 'Two', 'Three');
echo "<ol><li>" . implode("</li><li>", $arr1) . "</li></ol>";
?>

Output:

PHP implode

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:

PHP implode

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:

PHP implode

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:

PHP implode

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 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP Stack OverflowNext article:PHP Stack Overflow