虎泰克博客

PHP json_encode一个空值时返回NULL的原因以及解决办法


做项目时写的接口返回如下数据 {"status":"200","data":null} 其中 d

做项目时写的接口返回如下数据 {"status":"200","data":null} 其中 data字段返回的类型应该是数组。但是记录数为0的情况下,为何会返回 null 而不是 []呢?在网上查找了下资料原来是$data在数据为0的条件下$data就是一个未赋值的变量而非数组,想要更符合规范就得在输出前对$data进行检查,如果是null的话给$data赋值为空数组 $data=empty($data)?array():$data; 刷新接口返回正确数据 {"status":"200","data":[]}


Statement: all articles / drawings reproduced on this website are for the purpose of transmitting more information and facilitating discussion, which does not mean that the website, the host and the organizer agree with their opinions or confirm the authenticity of their contents. The contents of the articles are for reference only. If the rights of the original author are violated, please contact the website administrator.

Sign in to comment

Comment list

    No data