PHP json_encode 转换成空对象和空数组

$foo = array(
  "bar1" => (object)array(), // Should be encoded as an object
  "bar2" => array() // Should be encoded as an array
);

echo json_encode($foo);
// {"bar1":{}, "bar2":[]}


$obj = (object)[];
if ($obj) { //空对象 返回yes
    exit('yes');
} else {
    exit('no');
}

$array = [];
if ($array) { //空数组 返回no
    exit('yes');
} else {
    exit('no');
}

 

 

默认情况下用json_encode($foo)得到的是

{
  "bar1": [],
  "bar2": []
}

而加了JSON_FORCE_OBJECT参数的json_encode($foo,JSON_FORCE_OBJECT)得到的是

{
  "bar1": {},
  "bar2": {}
}

 

posted on 2018-01-19 19:45  dongruiha  阅读(402)  评论(0编辑  收藏  举报

导航