为什么json_decode为空数组返回null? [英] Why does json_decode return null for empty array?

查看:111
本文介绍了为什么json_decode为空数组返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这会回显"NULL"?我会把它解码成一个空数组.

Why would this echo "NULL"? In my would it would be decoded to an empty array.

我想念的东西明显吗?

<?php

$json = json_encode(array());
$json_decoded = json_decode($json, true);
// same with json_decode($json);

if ($json_decoded == null){
    echo "NULL";
} else
{
    echo "NOT NULL";
}

?>

推荐答案

这是因为array()==NULL.在这种情况下,它不会检查对象类型.

This is because array()==NULL. It does not check the object type in this case.

gettype(null)返回null,而

gettype(array())返回数组.希望你能有所作为.

gettype(array()) returns array. Hope you got the difference.

可能您需要的是

if ($json_decoded === null) {
   echo "NULL";
} else
{
   echo "NOT NULL";
}

这篇关于为什么json_decode为空数组返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆