为什么json_decode为空数组返回null ?

时间:2022-10-26 13:59:09

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

为什么这个echo会返回“NULL”?在我的意愿中,它将被解码为一个空数组。

Is it something obvious I'm missing?

有明显的东西我错过了吗?

<?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";
}

?>

5 个解决方案

#1


21  

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

这是因为数组()= = NULL。在这种情况下,它不检查对象类型。

gettype(null) returns null, whereas

方法返回null(null),而

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

方法(数组())返回数组。希望你能理解。

Probably what you need is

也许你需要的是

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

#2


2  

print_r the $json_decoded value it gives the empty array back. :)

print_r $json_decoded值,它将空数组返回。:)

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


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

outputs : Array ( ) This is because with == operator the empty array gets type juggled to null

输出:Array()这是因为使用==操作符时,空数组被转换为null。

#3


0  

This must do the trick

这一定很管用

 if ($json_decoded === null)

Example from the manual:

手册的例子:

<?php
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");

echo "Normal: ",  json_encode($a), "\n";
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

$b = array();

echo "Empty array output as array: ", json_encode($b), "\n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
?>

Output:

输出:

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}

#4


0  

You need to use strict equality operator ===, observe for yourself:

需要使用严格的等式运算符=== =,请自行观察:

$json = json_encode(array());
var_dump($json); // string(2) "[]"
$json_decoded = json_decode($json, true);
var_dump($json_decoded); // array(0) { }

So doing:

这样做:

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

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

would rightly go in else condition printing NOT NULL

在其他条件下打印是否正确

#5


0  

If your data includes some \n json_decode might fail silently too.

如果您的数据包含一些\n json_decode可能也会无声地失败。

#1


21  

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

这是因为数组()= = NULL。在这种情况下,它不检查对象类型。

gettype(null) returns null, whereas

方法返回null(null),而

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

方法(数组())返回数组。希望你能理解。

Probably what you need is

也许你需要的是

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

#2


2  

print_r the $json_decoded value it gives the empty array back. :)

print_r $json_decoded值,它将空数组返回。:)

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


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

outputs : Array ( ) This is because with == operator the empty array gets type juggled to null

输出:Array()这是因为使用==操作符时,空数组被转换为null。

#3


0  

This must do the trick

这一定很管用

 if ($json_decoded === null)

Example from the manual:

手册的例子:

<?php
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");

echo "Normal: ",  json_encode($a), "\n";
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

$b = array();

echo "Empty array output as array: ", json_encode($b), "\n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
?>

Output:

输出:

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

Empty array output as array: []
Empty array output as object: {}

Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}

#4


0  

You need to use strict equality operator ===, observe for yourself:

需要使用严格的等式运算符=== =,请自行观察:

$json = json_encode(array());
var_dump($json); // string(2) "[]"
$json_decoded = json_decode($json, true);
var_dump($json_decoded); // array(0) { }

So doing:

这样做:

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

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

would rightly go in else condition printing NOT NULL

在其他条件下打印是否正确

#5


0  

If your data includes some \n json_decode might fail silently too.

如果您的数据包含一些\n json_decode可能也会无声地失败。