20,359
社区成员
发帖
与我相关
我的任务
分享
[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]
$product_info=str_replace('"','"',$ship_info["product_info"]);//替换转义字符 (双引号)
$product_info=json_decode($product_info,true);
print_r($product_info);
[{"id":"9","model":"A2","quantity":"1"}]
[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]
$s = 'W3smcXVvdDtpZCZxdW90OzomcXVvdDs5JnF1b3Q7LCZxdW90O21vZGVsJnF1b3Q7OiZxdW90O0EyJnF1b3Q7LCZxdW90O3F1YW50aXR5JnF1b3Q7OiZxdW90OzEmcXVvdDt9LHsmcXVvdDtpZCZxdW90OzomcXVvdDs4JnF1b3Q7LCZxdW90O21vZGVsJnF1b3Q7OiZxdW90O0ExJnF1b3Q7LCZxdW90O3F1YW50aXR5JnF1b3Q7OiZxdW90OzEmcXVvdDt9XQ==';
$s = base64_decode($s);
$s = html_entity_decode($s);
$s = html_entity_decode($s); //多做几遍也无所谓,关键是要一层一层的将 html 转义转回来
echo $s;
print_r(json_decode($s, 1));
[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]
Array
(
[0] => Array
(
[id] => 9
[model] => A2
[quantity] => 1
)
[1] => Array
(
[id] => 8
[model] => A1
[quantity] => 1
)
)
$product_info=str_replace('"','"',$ship_info["product_info"]);
这行代码的问题。" 这样写一组数据的时候可以用。多组为空
" 这样写多组的正常组的。单组的为空
$product_info=str_replace('"','"',$ship_info["product_info"]);//替换转义字符 (双引号)
上面的数据是经这个替换的。
但为什么JSON里只有一级数据的时候是正常的呢。
$s = '[{"id":"9","model":"A2","quantity":"1"},{"id":"8","model":"A1","quantity":"1"}]';
print_r(json_decode($s, 1));
Array
(
[0] => Array
(
[id] => 9
[model] => A2
[quantity] => 1
)
[1] => Array
(
[id] => 8
[model] => A1
[quantity] => 1
)
)
没有问题
你应该贴出未经处理的原始数据,才可能发现问题
比如数据库取出的是 $ship_info["product_info"]
那就 echo base64_encode($ship_info["product_info"]);
贴出结果