How to do String to Array conversion in PHP
  John Mwaniki /   20 Dec 2021

How to do String to Array conversion in PHP

Given a scenario where you have a string of text, and the task of converting it into an array in PHP, this article explains the different ways of doing it with the help of multiple examples.

In this article, we will cover 2 different methods of doing it:

  1. Using the explode() function
  2. Using the str_split() function

1. Using the explode() function

The explode() function is a built-in function in PHP used to break a string into an array.

It splits a string into array elements based on the specified delimiter.

Syntax

explode(separator,string,limit)

Parameters

Parameter Requirement Description
separator Required This is a delimiter that specifies where to break the string. This can be anything such as a space (" "), comma(,), full stop(.), etc. The function splits the string where the delimiter (separator) exists. This parameter cannot be an empty string.
string Required Specifies the string in which we want to convert into an array.
limit Optional Specifies the number of array elements to return. If it is set to 0, the function returns an array with only one element. If it is set to greater than 0, the function returns an array with the maximum of the specified limit element(s). If it is less than 0, the function returns an array except for the last -limit elements().

Example

<?php
$str = "Do you really enjoy programming in PHP";
$array = explode(" ", $str);
var_dump($array);

Output

array(7) { [0]=> string(2) "Do" [1]=> string(3) "you" [2]=> string(6) "really" [3]=> string(5) "enjoy" [4]=> string(11) "programming" [5]=> string(2) "in" [6]=> string(3) "PHP" }

In the example above, we have converted the string "Do you really enjoy programming in PHP" into an indexed array with seven elements.

We have used space " " as the separator in the explode() function making the string to be broken at every instance with a space.

To check the size of the array, we use the count() or sizeof() functions.

Example

<?php
$str = "Do you really enjoy programming in PHP";
$array = explode(" ", $str);

echo "The size of the array is ".count($array);
echo "<br>";
echo "The size of the array is ".sizeof($array);

Output

The size of the array is 7
The size of the array is 7

We can access the elements of the resulting array using their respective array indices. The array indices start from zero. So to access the first element of the array, we will use index 0, to access the second, we use index 1, and so on.

Example

<?php
$str = "Do you really enjoy programming in PHP";
$array = explode(" ", $str);

echo "The value of index 4 is ".$array[4];
echo "<br>";
echo "The value of index 6 is ".$array[6];

Output

The value of index 4 is programming
The value of index 6 is PHP

In a similar way to the space above, we can use anything else as the separator.

Example

<?php
$cars = "BMW, Toyota, Volvo, Tesla, Nisan, Audi, Mazda";
$array = explode(",", $cars);

var_dump($array);

Output

array(7) { [0]=> string(3) "BMW" [1]=> string(7) " Toyota" [2]=> string(6) " Volvo" [3]=> string(6) " Tesla" [4]=> string(6) " Nisan" [5]=> string(5) " Audi" [6]=> string(6) " Mazda" }

In the above example, we have used comma (,) as the separator.

We can as well use the third (limit) parameter in the explode() function to limit the number of array elements we want to be returned.

Example

<?php
$dev = "All PHP developers are awesome!";
$array = explode(" ", $dev, 3);

$array = explode(" ", $dev, 3);

Output

Array ( [0] => All [1] => PHP [2] => developers are awesome! )

2. Using the str_split() function

The str_split() is another built-in function in PHP that splits a string into an array.

Unlike the explode() function that splits the string based on the specified separator, the str_split() function splits the string into equal parts of the provided length.

Syntax

str_split(string,length)

Parameters

Parameter Requirement Description
string Required Specifies the string in which we want to split into an array.
length Optional Specifies the length of each array element. Its default is 1.

The str_split() function returns FALSE if the specified length is less than 1. If the length is larger than the length of the string, the entire string will be returned as the only element of the array.

Example 1

<?php
$str = "Hi there! I love PHP";
$array = str_split($str, 3);
var_dump($array);

Output

array(7) { [0]=> string(3) "Hi " [1]=> string(3) "the" [2]=> string(3) "re!" [3]=> string(3) " I " [4]=> string(3) "lov" [5]=> string(3) "e P" [6]=> string(2) "HP" }

Example 2

<?php
$str = "Hello World!";
$array = str_split($str);
print_r($array);

Output

Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => W [7] => o [8] => r [9] => l [10] => d [11] => ! )

That's all.

That's how you convert a string into an array in PHP.