top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

PHP Implode and Explode functions?

+1 vote
1,993 views

PHP Implode - What is the use?

In PHP Implode function is used to join the elements of an array and returns joined element as a string. Primary use of php implode is with the echo statement where you may require to print the array and array can not be printed using the echo statement. Suppose you have an array as Array('A','E','I','O','U') and you want to print it as 'A-E-I-O-U' or something similar, implode can be handy in this case.

PHP Implode

PHP also provides the alias of Implode function as Join which is identical to implode in usage and behavior and both the functions are available in PHP4 and PHP5.

Explode PHP - What is the use?

Explode PHP function is used to split the string into in smaller string i.e. breaking the long sentence into small pieces and store the output into an array. Suppose you have a string as 'A-E-I-O-U' and you want to break that into Array('A','E','I','O','U') so that each one can be processed separately, php explode can be very handy.

PHP Explode

Syntax of Implode and Explode in PHP

string implode (array_strings); 
string implode (string_joiner,  array_of_strings);

string_joiner: It is the delimiter which is inserted in between of each element of array and if string_joiner is not present then it is taken as null string.
array_of_strings: It is array which needs to be converted to the string.
One beauty of the implode function is that it can accepts the argument in any order but for simplicity and less confusion it is recommended to use the argument in the provided order only.

array explode(delimiter, string, limit);
array explode(delimiter, string,);

delimiter: It will specify the boundary part and tells where to break the string.
String: This is input string which needs to be breaken.
limit: Using this argument you can fix what should be the maximum size of returned array. In Php 4.0.1 started positive values. and later php 5.1.0 it will support negative values.
Function returns the array of strings created by splitting the string based on delimiter. If delimiter is an empty string (""), explode returns FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

Comparison of PHP Implode and PHP Explode

When we compare the php implode function with php explode then we find that it is exact opposite what it means that if we supply the result of the implode function as one of the parameter of explode along with the delimiter as string_joiner used in the Implode function we will get the original array back.

Use Case of Implode in PHP

Normal Case with Joiner
In normal case joiner will be inserted between every element of the array (and it string is returned in the same order of array elements)

Example

$array = array('www', 'queryhome', 'com');

$dot_separated = implode(".", $array);
echo $dot_separated; 

$null_separated = implode("", $array);
echo $null_separated; 

$salil_saperated = implode("salil", $array);
echo $salil_separated; 

Output

tech.queryhome.com
wwwqueryhomecom
wwwsalilqueryhomesalilcom

Normal Case without Joiner
Example

$array = array('www', 'queryhome', 'com');

$null_separated = implode($array);
echo $null_separated;

Output

wwwqueryhomecom

Behavior with Null Array
If array is null the returned string is null irrespective of joiner (present or not present).
Example

$array = array();

echo "Output of implode is:".implode(".",$array);
echo "Output of implode is:".implode($array);

Output

Output of implode is:
Output of implode is:

Behavior with Multidimensional Array
Implode does not take the multidimentional array but individual implode can be applied before the master implode see the following example

$array = array(
  array("I", "am", "Salil"),
  array("I", "work", "for", "QueryHome")
  );

$string = array();
foreach ( $array as $val ) {
    $val = implode(' ', $val );
    $string[] = "{$val}";
}

echo implode(",", $string );

Output

I am Salil,I work for QueryHome

Implode by keys
Say if you want to return the which should have a specific order defined by the keys then we can first short the array based on the keys, followed by the implode operation see the following example -

const KEY0 = 1;
const KEY1 = 0;
const KEY2 = 3;
const KEY3 = 2;
const KEY4 = 4;
const KEY5 = 5;

$array = array(
    KEY0 => "The",
    KEY1 => "Sun",
    KEY2 => "rises",
    KEY3 => "in",
    KEY4 => "the",
    KEY5 => "East",
);
echo implode(" ", $array);

ksort($arr); // Sort the array based on the key
echo implode(" ", $array);

krsort($arr); // Sort the array in decending order based on the key
echo implode(" ", $array);

Output

The Sun rises in the East
Sun The in rises the East
East the rises in The Sun

Use Cases of Explode in PHP

Simple Example without using Limit

$string = 'QueryHome is a technical social Q&A';
print_r(explode(" ", $string));

Output:

Array
(
    [0] => QueryHome
    [1] => is
    [2] => a
    [3] => technical
    [4] => social
    [5] => Q&A
)

PHP Explode with using positive Limit
Lets us set the limit as 3. It will split the string as a three part.Lets see the code

$string = 'QueryHome is a technical social Q&A';
print_r(explode(" ", $string, 3));

Output:

Array
(
    [0] => QueryHome
    [1] => is
    [2] => a technical social Q&A
)

PHP Explode with Using Negative values

Here, we will set limit -3. It will reduce the word from last. Let see the code

$string = 'QueryHome is a technical social Q&A';
print_r(explode(" ", $string, -3));

Output:

Array
(
    [0] => QueryHome
    [1] => is
    [2] => a
)
posted Sep 25, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...