简体   繁体   English

如何使用php将以下字符串转换为数组

[英]How to Convert the below string into the array using php

How to convert the below string into array using php 如何使用PHP将以下字符串转换为数组

String : "[{\\"site_id\\":1,\\"posts\\":{\\"0\\":146,\\"2\\":104,\\"3\\":80},\\"groups\\":[{\\"group_id\\":1,\\"site_id\\":1,\\"group_name\\":\\"Default List\\",\\"posts\\":{\\"0\\":146,\\"2\\":104,\\"3\\":80}}]}]" 字符串: "[{\\"site_id\\":1,\\"posts\\":{\\"0\\":146,\\"2\\":104,\\"3\\":80},\\"groups\\":[{\\"group_id\\":1,\\"site_id\\":1,\\"group_name\\":\\"Default List\\",\\"posts\\":{\\"0\\":146,\\"2\\":104,\\"3\\":80}}]}]"

php code: json_decode(string, true) ; php代码: json_decode(string, true) ;

I am using above code to convert the string into an array, still no use 我正在使用上面的代码将字符串转换为数组, 仍然没有用

can know to how to convert the string into the array 可以知道如何将字符串转换为数组

I want to get the posts id from the string. 我想从字符串中获取帖子ID。

$jsonData = "[{\"site_id\":1,\"posts\":{\"0\":146,\"2\":104,\"3\":80},\"groups\":[{\"group_id\":1,\"site_id\":1,\"group_name\":\"Default List\",\"posts\":{\"0\":146,\"2\":104,\"3\":80}}]}]";    
$data = json_decode($jsonData);

It seems that your String is JSON - so you could translate that JSON into the appropriate datatype in php simply by using the built int json_decode function 看来您的String是json_decode因此您只需使用内置的int json_decode函数就可以将该JSON转换为php中的适当数据类型。

http://php.net/manual/de/function.json-decode.php http://php.net/manual/de/function.json-decode.php

$json = "['a','b','c']";
var_dump(json_decode($json), true);

produces 产生

array(3) {
[0] => 'a', 
[1] => 'b',
[2] => 'c'

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM