Convert String To Array And Array To String PHP

Overview

Arrays are used very often in PHP and at times you will need to convert a string into an array as well as converting an array into a string. PHP has two functions to achieve this and I will give an example of each one.

Simple use cases are splitting a string of names into an array containing all the names. Also converting an array of names, into a string which can be displayed to the user.

Convert String To Array And Array To String PHP

I will show you two ways to convert a string into an array, and a method to convert an array into a string.

  • explode()convert string to an array using a single delimiter
  • preg_split()convert string to an array using regex (more flexible and powerful)
  • implode()convert array to a string

explode()

Convert string to array using explode().

Converts a string into an array. String is split by each delimiter specified by the first parameter passed to the function. If you need to split a string into an array split by a single delimiter, then this method is ideal. If you need to use many delimiters or to split the string using a regex delimiter, then see preg_split() example below.

<?php

/* our string we are going to split (explode()) into an array */
$str = "Split:This:Into:An:Array:Please";
/* $arr will now be an array with the value of array("Split","This","Into","An","Array","Please"); */
$arr = explode(":",$str);

?>

preg_split()

Convert string to array using preg_split().

If you need to split a string into an array using regex, then take a look at the example below. It gives you the ability to convert a string into an array, however it also brings the power and flexibility of regex.

Using preg_split() allows you to split up a string at multiple delimiters. Take the string Tim, Sarah,Albert and Sue. In this case, we need to split the string up by “,” as well as ” and “.

<?php

/* our string we are going to split (preg_split()) into an array */
$str = "Tim, Sarah,Albert and Sue";

/* $arr will now be an array with the value of array("Tim", "Sarah", "Albert", "Sue"); */
$names = preg_split('/(,{1}[\s]?|\sand\s)+/', $str);

?>

implode()

Convert array to string using implode().

Convert an array into a string. Each Element of the array is glued together by the delimiter specified by the first parameter. For example, this is useful if you have an array of names that you want to convert to string to be displayed on a webpage.

<?php

/* our array we are going to glue together (implode()) into a string */
$arr = array("Glue","This","Into","A","String","Please");
/* $str will now be a string with the value "Glue This Into A String Please" */
$str = implode(" ",$arr); /* Note we glued it together with a space */

?>

And that is all there is to it.

Conclusion

While explode offers simple ways to convert string to an array, it lacks the power of preg_split() which allows you to specify the delimiter as a regex. However preg_split has more of an performance overhead as it has to process the regex.