In this tutorial, you will learn how to convert a string, comma-separated string to an array in PHP using explode().
How to Convert a String to Array in PHP?
The explode()
function is a built-in function in PHP that allows to easily convert a string into an array.
- Syntax of Explode Function
- Example 1: Converting a Comma Separated String to an Array
- Example 2: Converting a New Line Separated String to an Array
- Example 3: Converting a Pipe Separated String to an Array
- Example 4: Convert a String to an Array with a Different Delimiter
- Example 5: Convert a String with Multiple Delimiters to an Array
- Example 6: Convert Query String to an Array
Syntax of Explode Function
The explode() function takes two parameters: the delimiter and the string to be split.
array explode ( string $delimiter , string $string , int $limit = PHP_INT_MAX )
The parameters are:
- delimiter: Specifies the character(s) or string(s) that you want to use as a delimiter.
- string: Specifies the string that you want to convert to an array.
- limit (optional): Specifies the maximum number of elements that the array can have.
Example 1: Converting a Comma Separated String to an Array
Suppose you have a string that contains comma-separated values and you want to convert it into an array. In this example, you will use the explode() function to create an array of substrings separated by commas.
$string = "apple, banana, cherry, date, elderberry";
To convert this string into an array, you will use the explode() function and specify the comma as the delimiter:
$array = explode(",", $string);
The resulting array will look like this:
Array ( [0] => apple [1] => banana [2] => cherry [3] => date [4] => elderberry )
Example 2: Converting a New Line Separated String to an Array
In this example, you will convert a new line separated string into an array using the explode() function. Suppose you have the following string:
$string = "This is the first line.\nThis is the second line.\nThis is the third line.";
To convert this string into an array, you will use the explode() function and specify the new line character as the delimiter:
$array = explode("\n", $string);
The resulting array will look like this:
Array ( [0] => This is the first line. [1] => This is the second line. [2] => This is the third line. )
Example 3: Converting a Pipe Separated String to an Array
In this example, you will convert a pipe-separated string into an array using the explode() function. Suppose you have the following string:
$string = "John|Doe|25|Male|USA";
To convert this string into an array, you will use the explode() function and specify the pipe character as the delimiter:
$array = explode("|", $string);
The resulting array will look like this:
Array ( [0] => John [1] => Doe [2] => 25 [3] => Male [4] => USA )
Example 4: Convert a String to an Array with a Different Delimiter
You can use any character or string as a delimiter to split a string into an array. In this example, you will use a hyphen as a delimiter.
$string = "apple-banana-orange-grape";
$array = explode("-", $string);
print_r($array);
The output of this code would be:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
)
Example 5: Convert a String with Multiple Delimiters to an Array
You can also split a string into an array using multiple delimiters. In this example, you will use both a comma and a hyphen as delimiters.
$string = "apple-banana,orange-grape";
$array = preg_split("/[-,]/", $string);
print_r($array);
The output of this code would be:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
)
Example 6: Convert Query String to an Array
To convert a query string to an array in PHP using explode()
, you can first extract the query string from the URL using the $_SERVER['QUERY_STRING']
variable, and then split it into an array using the explode()
function.
Here’s an example code snippet to demonstrate this:
$url = "https://example.com/?fruit=apple&color=red&quantity=3"; $query_string = parse_url($url, PHP_URL_QUERY); // extract query string $array = explode('&', $query_string); // split query string into array // loop through the array to split each key-value pair into separate variables foreach ($array as $key_value) { list($key, $value) = explode('=', $key_value); $query_array[$key] = $value; // add key-value pair to the resulting array } print_r($query_array); // output the resulting array
To convert a query string to an array in PHP using explode()
, you can first extract the query string from the URL using the $_SERVER['QUERY_STRING']
variable, and then split it into an array using the explode()
function.
Here’s an example code snippet to demonstrate this:
phpCopy code$url = "https://example.com/?fruit=apple&color=red&quantity=3";
$query_string = parse_url($url, PHP_URL_QUERY); // extract query string
$array = explode('&', $query_string); // split query string into array
// loop through the array to split each key-value pair into separate variables
foreach ($array as $key_value) {
list($key, $value) = explode('=', $key_value);
$query_array[$key] = $value; // add key-value pair to the resulting array
}
print_r($query_array); // output the resulting array
The resulting output of the print_r()
function should be an associative array with the keys and values of the query string, like this:
Array ( [fruit] => apple [color] => red [quantity] => 3 )
Conclusion
The explode() function is a very useful tool in PHP for converting a string into an array.
Recommended PHP Tutorials
- PHP Convert String to Array
- PHP Array: Indexed,Associative, Multidimensional
- To Remove Elements or Values from Array PHP
- PHP remove duplicates from multidimensional array
- Remove Duplicate Elements or Values from Array PHP
- Array Push and POP in PHP | PHP Tutorial
- PHP Search Multidimensional Array [key and value and return key
- php array to comma separated list
- PHP Remove Duplicate Elements or Values from Array PHP
- Reverse String in PHP