将带有方括号的字符串转换为PHP数组
我有一个字符串,值放在方括号中,并用逗号分隔,例如数组字符串:
I have a string, values are in brackets and separated with comma, like array string:
示例:
[[["Name" , "ID"] , [12]] , ["Test" , 78] , 0]
如何将此字符串转换为PHP数组?
How to convert this string to PHP array?
这是 JSON 的字符串表示形式数组,请使用json_decode()
:
That's a JSON string representation of an array, use json_decode()
:
$array = json_decode($string);
print_r($array);
收益:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Name
[1] => ID
)
[1] => Array
(
[0] => 12
)
)
[1] => Array
(
[0] => Test
[1] => 78
)
[2] => 0
)
如果它具有任何将成为对象的{ }
并且在PHP中将其解码为stdClass
对象,除非您将true
传递给json_decode()
以强制使用数组.
If it had any { }
that would be an object and decoded as a stdClass
object in PHP unless you pass true
to json_decode()
to force an array.
由于它被构造为一个PHP数组(从PHP 5.4开始),因此也可以正常工作(不要对不可信的数据使用eval
).绝对没有理由这样做,只是为了好玩:
Since it's structured as a PHP array (as of PHP 5.4), this works as well (don't use eval
on untrusted data). There is absolutely no reason to do this, it's just for fun:
eval("\$array = $string;");
print_r($array);