Why does json_decode return null for empty array?

后端 未结 5 1206
萌比男神i
萌比男神i 2021-01-12 00:27

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

Is it something obvious I\'m missing?



        
5条回答
  •  鱼传尺愫
    2021-01-12 01:12

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

    $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

提交回复
热议问题