AI摘要

文章讨论了PHP中将JSON字符串转换为数组时遇到的问题及其解决方法。问题在于使用json_decode函数时,未将第二个参数设为true,导致结果为对象而非数组,无法使用数组语法访问。解决方法有两种:1. 使用json_decode(d,true)2.使访d, true)将结果转换为数组;2. 使用对象访问方式,如res->key。
文章讨论了PHP中将JS

将session里的json字符串转换为数组
//a:3:{s:4:"name";s:6:"huqian";s:3:"sex";s:3:"男";s:3:"age";i:27;}
$whereSql = json_decode(getSession('whereSql'));
$whereSql['name'];

出现错误:
Cannot use object of type stdClass as array

解决方法(2种):
1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true,转换为数组
2、json_decode(res)使res) 返回的是一个对象, 不可以使用 res['key'] 进行访问, 换成 $res->key 就可以了



最后修改:2020 年 05 月 05 日
点赞的人是最酷的