PHP implode and explode functions are very easy to use. In this tutorial, we will learn how to do it. Let’s check it out.

There are some situations where we need to convert a string to array and vice versa to work with our logic, here implode and explode functions comes in handy. It’s easy to learn and implement.

The implode function implodes an array into a string, and the explode function explodes a string into an array. It’s very useful if you’ve stored your data in strings in files and want to convert those strings into array elements when your Web application runs.

Let’s take a look at an example below:

<?php

$flower[0]=”Lily”;
$flower[1]=”Rose”;
$flower[2]=”Tulip”;
$text=implode(“, “,$flower);
echo $text;

?>

 

This gives: Lily, Rose, Tulip

As we can see, we specify the string that separates the array items by “, “ (in this case).

So the syntax of implode is  $text=implode([string separator],[array]);

Now, how about exploding this string back to the array? It’s very simple, to do that, we have to indicate which text we want to split, like “, “.

<?php

$text=”Lily, Rose, Tulip”;
$flower=explode(“, “,$text);
Print_r($flower);

?>

 

This gives :

Array
(
[0] => Lily
[1] => Rose
[2] => Tulip
)

The syntax of explode is $array=explode([string separator],[string]);

So as we can see, it’s nice and simple.

If you have any issue, let us know in the comment box.

Happy Coding.