

The following example illustrates the working of the explode() function to convert string to array in PHP. Example Input:Įxplode(“ “, “Hello, what is your name?") Output:Įxplode(“ “, “Hello, what is your name?", 3) Output:Įxplode(“ “, “Hello, what is your name?", -1) Output: The explode() function returns an array that contains the string pieces as its elements.

Positive: If the passed integer is positive, then the array will store this many numbers of elements.This number can be positive, negative, or zero. This parameter represents the number of strings in which it should split the original string. $no_of_elements (optional): This is the last and an optional parameter that is passed to this function.$initial_name: The second parameter that is passed to this function is the string that has to be converted into an array.$separator: The separator is a character that commands the explode() function to split the string whenever it detects the separator and stores that substring in the array.

After splitting the string into smaller substrings, this function stores them in an array and returns the array.Įxplode($separator, $initial_string, $no_of_elements) Parameters This separator could be a comma (,), a dot (.), or anything. Unlike the str_split() function, this function uses a separator or delimiter that needs to be passed as an argument to the function. The explode() function is another method of PHP that is used to convert a string into an array. And the following expression passes 3 as the length delimiter, which converts the substring of length 3 into array elements. So, it converts separate elements of the string into array elements. The following expression passes the string to this method without passing the length argument.īy default, if you do not pass the length delimiter, it takes it as 1. It uses the str_split() method to convert the string to an array. In the above example, it initializes a variable $my_string1 with a string “Sample String”. Print_r($my_array2) // sam, ple, str, ing The following example illustrates the working of the str_split() function to convert string to array in PHP.Įcho "The array of length 3 elements is: "

If the length that is passed to the function surpasses the length of the initial string, the function will return the whole string as one element, whereas if the length integer is less than one, the function will return false. This function returns an array that contains the pieces of the original string. If not passed, the function will consider this length as 1 by default.
#Php convert array to string manual#
There are various approaches, including in-built functions and manual approaches that are used to convert string to array in PHP. This article explains several approaches to achieve this task.įollowing is the complete list of methods that can be used in PHP to convert a string to an array.ĭifferent Methods to Convert String to Array in PHP Being a powerful scripting language, PHP provides various in-built methods to convert a string to an array. When you convert a string into an array, it makes data manipulation convenient. Arrays can help you implement better data organization and faster operations. This provides more security and ease of access to the data. For instance, you can convert the passwords entered as input in a web application into an array before storing them in the database. There are many cases when an array is considered more suitable over a string. Why Do You Need to Convert a String to an Array in PHP?
