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

How to convert array string to array in php

PHPz
PHPzOriginal
2023-04-27 09:08:22893browse

In PHP, we can use some functions or methods to easily convert an array in string format into a real array variable. In this article, we will introduce some commonly used methods and functions.

  1. Use the eval function
    Use the PHP built-in function eval() to execute the string as executable code, so you can use the eval function to convert an array in string format into a real array .

For example:

$str = "[1,2,3]";
$array = eval("return $str;");
print_r($array);

In the above example, we assign an array "[1,2,3]" containing 3 elements to the string variable $str, Then use eval() to execute the string variable as executable code, and finally get a real array $array. You can use the print_r() function to print the result. The result is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Although the eval() function can It is very convenient to convert an array string into an array, but it also brings certain security risks because it can execute arbitrary code, including malicious code, so eval() is not recommended.

  1. Use json_decode function
    In PHP, we can also use the json_decode() function to convert an array in string format into a real array variable.

For example:

$str = "[1,2,3]";
$array = json_decode($str);
print_r($array);

In the above example, we use the json_decode() function to convert the string variable $str into the array variable $array, and finally get a real array, You can use the print_r() function to print the results. The results are as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

json_decode() function also has an optional parameter $assoc. When the $assoc parameter is set to true, the returned array will be an associative array. Non-numeric index array. For example:

$str = '{"name":"Tom","age":18}';
$array = json_decode($str,true);
print_r($array);

In the above example, we convert a JSON string "$str" containing two key-value pairs "name" and "age" into an associative array "$array". The result is as follows :

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

Therefore, if you need to convert an array in string format to an associative array, you can set the $assoc parameter to true.

  1. Use the explode function
    Use the PHP built-in function explode() to split a string separated by a specific delimiter into an array.

For example:

$str = "1,2,3";
$array = explode(",", $str);
print_r($array);

In the above example, we convert a comma-separated string "1,2,3" into an array, using the explode() function to The comma is used as the delimiter to split, and finally a real array is obtained. The print_r() function can be used to print the result. The result is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

It should be noted that using the explode() function only applies to cases where the delimiter is In the case of fixed values, if the delimiter is not fixed or multi-dimensional arrays need to be parsed, other methods need to be used.

To sum up, the above three methods can be used to convert an array in string format into a real array variable. In actual use, we need to choose the appropriate method according to the specific situation. When the format of the array string is relatively standardized and will not be subject to security attacks, it is best to use the json_decode() function; when the format of the array string is relatively free or when multi-dimensional arrays need to be parsed, other methods can be used.

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