How to convert php string to array

PHPz
Release: 2023-04-25 14:08:45
Original
583 people have browsed it

In PHP, we often need to convert strings into arrays for operations, such as parsing URL parameters, parsing JSON data, etc. This article will show you how to convert a string into an array using some functions in PHP.

  1. Use explode() function

explode() function can split a string into an array according to the specified delimiter. For example, we have a comma-separated string, which can be converted into an array using the explode() function:

$str = "apple,banana,cherry";
$arr = explode(",", $str);
print_r($arr);
Copy after login

The output will be as follows:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)
Copy after login

In addition to commas, other characters can also be used as Delimiters such as spaces, colons, vertical bars, etc.

  1. Use preg_split() function

If we need to use regular expressions to split strings, we can use the preg_split() function. For example, we have a string similar to URL parameters, which can be split into arrays according to '&' and '=' using the preg_split() function:

$str = "name=John&age=30&gender=male";
$arr = preg_split("/[&=]/", $str);
print_r($arr);
Copy after login

The output is as follows:

Array
(
    [0] => name
    [1] => John
    [2] => age
    [3] => 30
    [4] => gender
    [5] => male
)
Copy after login

In the preg_split() function, the regular expression "/[&=]/" means splitting the string according to '&' or '='.

  1. Use the str_split() function

The str_split() function can split a string into strings of specified length and store them in an array. For example, we have a string, and we can use the str_split() function to store each of its characters into an array:

$str = "Hello World";
$arr = str_split($str);
print_r($arr);
Copy after login

The output is as follows:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
Copy after login

Except that each character is stored in In the array, you can also specify the length of each string. For example, split the string into strings with a length of 2:

$str = "Hello World";
$arr = str_split($str, 2);
print_r($arr);
Copy after login

The output result is as follows:

Array
(
    [0] => He
    [1] => ll
    [2] => o 
    [3] => Wo
    [4] => rl
    [5] => d
)
Copy after login
  1. Use json_decode( ) Function

If we need to convert a JSON format string into an array or object, we can use the json_decode() function. For example, there is a string in JSON format:

{
    "name": "John",
    "age": 30,
    "gender": "male"
}
Copy after login

You can use the json_decode() function to convert it into an array:

$str = '{"name":"John","age":30,"gender":"male"}';
$arr = json_decode($str, true);
print_r($arr);
Copy after login

The output result is as follows:

Array
(
    [name] => John
    [age] => 30
    [gender] => male
)
Copy after login
Copy after login

Note that the first The two parameters should be set to true, which means converting JSON into an associative array. If set to false, it will be converted to an object.

  1. Use the parse_str() function

If we need to parse a string similar to a URL parameter into an array, we can use the parse_str() function. For example, there is a string similar to a URL parameter:

$str = "name=John&age=30&gender=male";
Copy after login

You can use the parse_str() function to convert it to an associative array:

parse_str($str, $arr);
print_r($arr);
Copy after login

The output is as follows:

Array
(
    [name] => John
    [age] => 30
    [gender] => male
)
Copy after login
Copy after login

In the parse_str() function, the first parameter is the string to be parsed, and the second parameter is the name of the array that stores the parsed results.

Summary

This article introduces 5 methods of converting strings to arrays in PHP: using the explode() function, using the preg_split() function, using the str_split() function, and using json_decode( ) function, use the parse_str() function. Choosing different methods according to different situations can conveniently handle strings.

The above is the detailed content of How to convert php string 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!