Home > Backend Development > PHP Problem > How to convert php array string to array

How to convert php array string to array

PHPz
Release: 2023-04-18 09:39:59
Original
1243 people have browsed it

In PHP, we often need to split strings into arrays, or convert arrays into strings for storage or output display. Below are several common methods for converting array strings into arrays.

Method 1: explode function

PHP’s explode function can split a string into an array according to the specified delimiter. Its basic usage is as follows:

$array = explode($delimiter, $string);
Copy after login

$delimiter is the delimiter, which can be a single character or a string; $string is the string to be split. For example, we have the following string:

$string = 'apple,banana,orange';
Copy after login

We can use commas as delimiters to split the string into an array:

$array = explode(',', $string);
print_r($array);
Copy after login

The output is as follows:

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

Method 2: str_split function

PHP’s str_split function can split a string into an array of multiple characters. Its basic usage is as follows:

$array = str_split($string, $length);
Copy after login

$string is the string to be split, $length is the length of each character block, which can be omitted and defaults to 1. For example, we have the following string:

$string = 'hello world';
Copy after login

We can split the string into character blocks of length 3:

$array = str_split($string, 3);
print_r($array);
Copy after login

The output is as follows:

Array
(
    [0] => hel
    [1] => lo 
    [2] => wo
    [3] => rl
    [4] => d
)
Copy after login

Method Three: preg_split function

PHP’s preg_split function can split a string into an array according to a regular expression. Its basic usage is as follows:

$array = preg_split($pattern, $string, $limit, $flags);
Copy after login

$pattern is a regular expression, $string is the string to be split. $limit is optional and specifies the maximum number of splits. If omitted or negative, it will be split as many times as possible. $flags is also optional and is used to specify the behavior of the preg_split function, such as whether to ignore case, etc. For example, we have the following string:

$string = '2019-11-16 12:30:00';
Copy after login

We can use regular expressions to split the string into two parts: date and time:

$array = preg_split('/\s+/', $string);
print_r($array);
Copy after login

The output is as follows:

Array
(
    [0] => 2019-11-16
    [1] => 12:30:00
)
Copy after login

Method 4: json_decode function

If we have converted an array into a JSON format string, we can use the json_decode function to convert it back. The json_decode function can convert a string in JSON format into a PHP array or object. Its basic usage is as follows:

$array = json_decode($string, $assoc);
Copy after login

$string is a string in JSON format. $assoc specifies whether the return result is an associative array and can be omitted. , the default is false. For example, we have the following string in JSON format:

$string = '{"name":"Tom","age":20}';
Copy after login

We can convert this string into a PHP array:

$array = json_decode($string, true);
print_r($array);
Copy after login

The output is as follows:

Array
(
    [name] => Tom
    [age] => 20
)
Copy after login

That’s it Several common methods of converting array strings into arrays. In actual development, we can choose the appropriate method to convert between strings and arrays according to specific needs.

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