php strings can be converted into arrays. 5 conversion methods: 1. Use "str_split (string, element length)" to convert the string into an array; 2. Use "explode (separator, string)" to convert the string into an array; 3. Use "preg_split" ('match pattern', string, -1, $flags)" to convert; 4. Use "settype (string, "array")" to convert; 5. Add parentheses before the string variable Target type "(array)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
The string in php can be converted into array. In php, there are many ways to convert strings into arrays.
Method 1: Use the str_split() function
The str_split() function splits the string into an array, that is, converts the string into a character array.
str_split(string,length)
The parameter description is as follows:
string Required. Specifies the string to be split.
#length Optional. Specifies the length of each array element. The default is 1.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组:"; $arr1=str_split($str); var_dump($arr1); $arr2=str_split($str,2); var_dump($arr2); ?>
Method 2: Use the explode() function
explode() function can be based on strings The delimiter splits the string, that is, it splits a string into several substrings based on the delimiter, and then combines these substrings into an array and returns it.
explode($delimiter, $string [, $limit])
The parameter description is as follows:
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = 'hypertext language programming'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组(分割符为空格):"; $arr=explode(" ",$str); var_dump($arr); ?>
Method 3. Use the preg_split() function--- Convert the string into an array
preg_split() function splits the string through a regular expression.
preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
The parameter description is as follows:
Note: This will change each element in the returned array so that each element becomes an array consisting of the 0th element being the separated substring and the 1st element being the offset of the substring in the subject. .
Return value: Returns an array composed of substrings obtained after using $pattern to split the subject string.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = '1 2 3 4,5 6-7 8=9'; var_dump($str); $arr=preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE); var_dump($arr); ?>
Method 4: Use the settype() function
The settype() function can re Set the type of the variable and convert the type of the string variable to an array.
settype($var,$type)
The parameter description is as follows:
$var: the variable to be converted.
$type: required data type.
Just set $type to ""array"" to set the variable $var to the array type.
Note: The settype() function will change the original variable, returning TRUE if the setting is successful and FALSE if it fails.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组类型:"; $arr=settype($str,"array"); var_dump($str); var_dump($arr); ?>
Method 5: Add the target type "(array)" enclosed in brackets before the string variable
(array): Force the specified variable into an array type;
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组类型:"; $arr=(array)$str; var_dump($arr); ?>
推荐学习:《PHP视频教程》
The above is the detailed content of Can strings be converted into arrays in php?. For more information, please follow other related articles on the PHP Chinese website!