将方括号转换为PHP数组

时间:2022-06-16 01:55:07

I have a string, values are in brackets and separated with comma, like array string:

我有一个字符串,值在括号中并用逗号分隔,如数组字符串:

Example:

[[["Name" , "ID"] , [12]] , ["Test" , 78] , 0]

How to convert this string to PHP array?

如何将此字符串转换为PHP数组?

1 个解决方案

#1


12  

That's a JSON string use json_decode():

这是一个JSON字符串使用json_decode():

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

Yields:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Name
                    [1] => ID
                )

            [1] => Array
                (
                    [0] => 12
                )

        )

    [1] => Array
        (
            [0] => Test
            [1] => 78
        )

    [2] => 0
)

Since it's structured as a PHP array (as of PHP 5.4), this works as well (don't use eval on untrusted data):

由于它的结构是一个PHP数组(从PHP 5.4开始),这也适用(不要在不受信任的数据上使用eval):

eval("\$array = $string;");
print_r($array);

#1


12  

That's a JSON string use json_decode():

这是一个JSON字符串使用json_decode():

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

Yields:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Name
                    [1] => ID
                )

            [1] => Array
                (
                    [0] => 12
                )

        )

    [1] => Array
        (
            [0] => Test
            [1] => 78
        )

    [2] => 0
)

Since it's structured as a PHP array (as of PHP 5.4), this works as well (don't use eval on untrusted data):

由于它的结构是一个PHP数组(从PHP 5.4开始),这也适用(不要在不受信任的数据上使用eval):

eval("\$array = $string;");
print_r($array);