php convert to array

PHPz
Release: 2023-05-23 11:05:36
Original
647 people have browsed it

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.

$str = "Hello world PHP";
$arr ​​= explode(" ", $str);
print_r($arr);
? >

The above code will output the following results:

Array
(

[0] => Hello
[1] => world
[2] => PHP
Copy after login
Copy after login

)

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.

$str = "Hello, world, PHP";
$arr ​​= preg_split('/[s,] /', $str);
print_r( $arr);
?>

The above code will output the following results:

Array
(

[0] => Hello
[1] => world
[2] => PHP
Copy after login
Copy after login

)

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.

$str = "name=john&age=20&gender=male";
parse_str($str, $arr);
print_r($arr);
?>

The above code will output the following result:

Array
(

[name] => john
[age] => 20
[gender] => male
Copy after login

)

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.

$arr ​​= array("name" => "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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!