Home  >  Article  >  Backend Development  >  Can strings be converted into arrays in php?

Can strings be converted into arrays in php?

青灯夜游
青灯夜游Original
2022-07-26 15:25:478962browse

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)".

Can strings be converted into arrays in php?

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hello&#39;;
echo "原字符串:";
var_dump($str);
echo "将字符串转为数组:";
$arr1=str_split($str);
var_dump($arr1);
$arr2=str_split($str,2);
var_dump($arr2);
?>

Can strings be converted into arrays in php?

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:

  • $delimiter: The delimiter character used to split the string;
  • $string: The string that needs to be split;
  • $limit: Optional parameter, can be empty, specifies the number of array elements to be returned;
    •             If $limit is not empty and is a positive number, the returned array contains at most $limit elements, and the last element contains the remainder of $string;
    •             If $limit is not empty and is negative, all elements except the last $limit elements are returned;
    •             If $limit is 0, it will be treated as 1;
    •           If $limit is empty, all array elements are returned.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hypertext language programming&#39;;
echo "原字符串:";
var_dump($str);
echo "将字符串转为数组(分割符为空格):";
$arr=explode(" ",$str);
var_dump($arr);
?>

Can strings be converted into arrays in php?

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:

  • $pattern: The pattern used for matching, that is, the regular expression.
  • $subject The string to be split.
  • $limit: Optional parameter. If specified, the substrings obtained by limiting the separation will be limited to at most limit, and the last substring will contain all remaining parts. When the limit value is -1, 0 or NULL, it means "no limit". It is recommended to use NULL.
  • $flags: optional parameter, which has 3 values.
    • If set to PREG_SPLIT_NO_EMPTY, preg_split() will return the separated non-empty part.
    • If set to PREG_SPLIT_DELIM_CAPTURE, bracket expressions in delimited patterns will be captured and returned.
    • If set to PREG_SPLIT_OFFSET_CAPTURE, the string offset will be appended to the return for each occurrence of a match.

              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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;1 2 3 4,5 6-7 8=9&#39;;
var_dump($str);
$arr=preg_split(&#39;/ /&#39;, $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
var_dump($arr);
?>

Can strings be converted into arrays in php?

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hello world!&#39;;
echo "原字符串:";
var_dump($str);
echo "将字符串转为数组类型:";
$arr=settype($str,"array");
var_dump($str);
var_dump($arr);
?>

Can strings be converted into arrays in php?

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hello world!&#39;;
echo "原字符串:";
var_dump($str);
echo "将字符串转为数组类型:";
$arr=(array)$str;
var_dump($arr);
?>

Can strings be converted into arrays in php?

推荐学习:《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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn