Home  >  Article  >  Backend Development  >  How to convert php array string to array

How to convert php array string to array

PHPz
PHPzOriginal
2023-04-18 09:06:301168browse

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);

$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';

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

$array = explode(',', $string);
print_r($array);

The output is as follows:

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

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);

$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';

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

$array = str_split($string, 3);
print_r($array);

The output is as follows:

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

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);

$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';

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

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

The output is as follows:

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

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);

$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}';

We can convert this string into a PHP array:

$array = json_decode($string, true);
print_r($array);

The output is as follows:

Array
(
    [name] => Tom
    [age] => 20
)

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!

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