PHP How to Convert String to Array Easily
In PHP, you can convert a string to an array using
explode(separator, string) to split by a delimiter or str_split(string) to split into characters.Examples
Input"apple,banana,orange"
Output["apple", "banana", "orange"]
Input"hello"
Output["h", "e", "l", "l", "o"]
Input"one|two|three"
Output["one", "two", "three"]
How to Think About It
To convert a string to an array, first decide how you want to split it: by a specific character like a comma or by each character. Use
explode if you have a clear separator, or str_split to split every character into array elements.Algorithm
1
Get the input string.2
Decide the splitting method: by delimiter or by characters.3
If splitting by delimiter, use explode with the delimiter and string.4
If splitting by characters, use str_split with the string.5
Return the resulting array.Code
php
<?php // Convert string to array by delimiter $string = "apple,banana,orange"; $array = explode(",", $string); print_r($array); // Convert string to array by characters $string2 = "hello"; $array2 = str_split($string2); print_r($array2); ?>
Output
Array
(
[0] => apple
[1] => banana
[2] => orange
)
Array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)
Dry Run
Let's trace converting "apple,banana,orange" to an array using explode.
1
Input string
string = "apple,banana,orange"
2
Call explode
explode(",", string) splits string at each comma
3
Result array
["apple", "banana", "orange"]
| Iteration | Current Part |
|---|---|
| 1 | apple |
| 2 | banana |
| 3 | orange |
Why This Works
Step 1: Using explode
explode splits the string at each occurrence of the delimiter and returns an array of parts.
Step 2: Using str_split
str_split breaks the string into single characters, each becoming an array element.
Alternative Approaches
preg_split
php
<?php $string = "one,two;three four"; $array = preg_split('/[ ,;]+/', $string); print_r($array); ?>
Use <code>preg_split</code> to split by multiple delimiters using regular expressions, but it's slower than explode.
mb_str_split
php
<?php
$string = "héllo";
$array = mb_str_split($string);
print_r($array);
?>Use <code>mb_str_split</code> for multibyte-safe splitting into characters, useful for UTF-8 strings.
Complexity: O(n) time, O(n) space
Time Complexity
Splitting the string requires scanning each character once, so time is proportional to string length, O(n).
Space Complexity
The output array stores parts or characters, so space used grows with string length, O(n).
Which Approach is Fastest?
explode is fastest for simple delimiter splits; str_split is efficient for character splits; preg_split is slower due to regex overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| explode | O(n) | O(n) | Splitting by single delimiter |
| str_split | O(n) | O(n) | Splitting into characters |
| preg_split | O(n) | O(n) | Splitting by multiple delimiters or patterns |
| mb_str_split | O(n) | O(n) | Splitting multibyte UTF-8 characters |
Use
explode when you know the delimiter, and str_split to split every character.Trying to split a string without specifying the correct delimiter or using
explode without a delimiter causes errors.