Home > Article > Backend Development > php convert to array
In PHP programming, arrays are often used. Storing a set of data in a variable can make the program more flexible and controllable. In actual development, we sometimes need to convert a piece of text or string into an array. This article will introduce how to use PHP to convert a string into an array, as well as some practical tips and methods.
1. Use the PHP built-in function explode() to convert a string into an array
The PHP built-in function explode() can convert a string into an array. This function splits a string into an array, split by the specified delimiter. For example, we have a string that contains three words "Hello world PHP". We can convert it into an array using " " spaces as delimiters.
c1edade2619e17a1aabdbcb40d98c47f
The above code will output the following results:
Array
(
[0] => Hello [1] => world [2] => PHP
)
In the above code, we use explode() The function converts the $str string into an array, using spaces as delimiters. This function returns an array where each data element is a string separated by a delimiter.
2. Use regular expression preg_split() to convert a string into an array
PHP built-in function preg_split() uses a regular expression to split a string and returns an array. If you need more flexible control over separators, you can use the preg_split() function. For example, we have a string containing commas and spaces, we can convert it into an array using the regular expression /[s,] / as delimiter.
b529e11126d2d05090c49e3cc7c630c7
The above code will output the following results:
Array
(
[0] => Hello [1] => world [2] => PHP
)
In the above In the code, we use the preg_split() function to convert the $str string into an array, and use the regular expression /[s,] / as the separator. This function returns an array where each data element is a string matched by the regular expression.
3. Convert URL parameters into arrays
In web development, we often need to convert the parameters in the URL into arrays. For example, we have a URL string that contains several parameters. We can use the parse_str() function to convert it into an array.
c0dde3fa6bfc3f24c18aed3d0396c8f2
The above code will output the following result:
Array
(
[name] => john [age] => 20 [gender] => male
)
In the above code, we use parse_str( ) function converts the $str string into an array. This function accepts two parameters, the first parameter is the string that needs to be converted into an array, and the second parameter is an optional result array variable name. If the second argument is not provided, the function returns an array.
4. Convert associative arrays into strings
In addition to converting strings into arrays, sometimes we also need to convert arrays into strings. An array can be converted into a string using the built-in function implode(). For example, we have an associative array containing three elements, and we can convert it into a string using "_" as the delimiter.
fc80601fb91269732cd93652ae9f5ed3 "john", "age" => 20, "gender" => "male");
$str = implode("_", $arr);
echo $str;
?>
The above code will output the following results:
john_20_male
In the above code, we use the implode() function to convert the $arr array into a string, and use "_" as the delimiter.
Summary
This article introduces several methods of converting strings into arrays in PHP, including using the built-in functions explode() and preg_split(), and parse_str to convert URL parameters into arrays ()function. Additionally, we introduced the built-in function implode() that converts an associative array into a string. These methods are very useful techniques and methods in PHP programming, which are very helpful in improving programming efficiency and code readability.
The above is the detailed content of php convert to array. For more information, please follow other related articles on the PHP Chinese website!