Home >Backend Development >PHP Problem >How to convert string to array in php
In PHP, string is a very common data type. Usually we need to split the string into a set of characters and store them in an array. for processing and processing in the following code. Here are some methods to convert a PHP string to a PHP array:
Method 1: Use the str_split function
The str_split function can convert a string into an array of single characters. The syntax of this function is as follows:
array str_split ( string $string [, int $split_length = 1 ] )
Among them, the string parameter is the string to be converted, and the split_length parameter is the length of each element in the array to be generated (default is 1). For example:
$str = "Hello world"; $arr1 = str_split($str); $arr2 = str_split($str, 3); print_r($arr1); print_r($arr2);
Output result:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => w [7] => o [8] => r [9] => l [10] => d ) Array ( [0] => Hel [1] => lo [2] => wor [3] => ld )
Method 2: Use str_split and array_map functions combined
If you want to convert a string into a numeric array, you can use the following method :
$str = '123456789'; $arr = array_map('intval', str_split($str)); print_r($arr);
Output result:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
Method 3: Use preg_split function
If you need to split the string into an array according to the specified delimiter or regular expression, You can use the preg_split function. This function returns an array whose elements are substrings divided by strings. The syntax of this function is as follows:
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
Among them, pattern is the regular expression to be matched, subject is the string to be split, the limit parameter specifies the maximum number of elements to be generated, and flags specifies the regular expression flag. For example:
$str = "apple,banana,orange"; $arr = preg_split("/[\s,]+/", $str); print_r($arr);
Output result:
Array ( [0] => apple [1] => banana [2] => orange )
Method 4: Use the explode function
Similar to preg_split, if you need to split the string into an array according to the specified delimiter , you can use the explode function. The syntax of this function is as follows:
array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
Among them, the delimiter parameter is the specified delimiter, the string parameter is the string to be split, and the limit parameter specifies the maximum number of elements to be generated. For example:
$str = "apple,banana,orange"; $arr = explode(",", $str); print_r($arr);
Output result:
Array ( [0] => apple [1] => banana [2] => orange )
The above are several methods of converting PHP strings into PHP arrays. You can choose the appropriate method and apply it flexibly according to your needs.
The above is the detailed content of How to convert string to array in php. For more information, please follow other related articles on the PHP Chinese website!