What is the PHP implode function
An implode is the process where we convert and join the pieces of characters into a single string or sentence. The string pieces are the smallest parts of characters which you will get after the use of the PHP explode function.
PHP implode function reverses the modification of the sentence which you have obtained from PHP explode function.
Suppose there are some pieces of characters like “This”,”is”, “the”, “great”, “world” and after you use implode it converts the small pieces of elements into a string as given below:
1. This
2. is
3. the
4. great
5. world
The implode function group the array elements into string.
Why we use PHP implode() function
We can use the PHP implode function to convert an array elments into string of characters. A string of characters can be a group of words which you can call it as a sentence.
PHP implode is useful when you want to reverse the PHP explode effect and get some string of characters. The syntax of the PHP implode function is as given below:-
Syntax
1 2 3 |
<?php implode(String $separtor, String $array); ?> |
Check below table for the description of each parameter of the implode function of PHP.
Parameter Name | Description |
---|---|
$separator |
|
$array |
|
Example of PHP implode() function converts array element into string
The below example contains the array which you can convert into a string using the implode function. Here in the given example, we are using the space as a separator between the string characters.
The output of this example is the sentence with some spaces between the words.
Example 1:
1 2 3 4 5 |
<?php $array = array('This','is','Tutorialdeep','learning','PHP','function.'); $output = implode(" ",$array); echo $output; ?> |
Output
Example2: The example contains the dash(-) as the separator after you apply the implode function to the array. There are seven array values which you can combine using the PHP implode function.
Here, We are using the PHP echo statement to print the output of the implode function of the PHP.
1 2 3 4 5 |
<?php $languageArray = array("PHP","is","a","server","side","scripting","language."); $Languagoutput = implode("-",$languageArray); echo $Languagoutput; ?> |
Output
Example 3 : You can also use any other separator like comma and a single space(, ). After you use the implode function with the separator, it will convert the array into a string of characters with the separator you want to to put among the chraracters.
1 2 3 4 |
<?php $count = array("200","500","101","209","767","485","987"); $countarray = implode(", ",$count); ?> |
Output
Reference
You must also read:-