简体   繁体   中英

How to initialize an empty associative array in PHP?

I want to have an empty object in the API response like the one mentioned below:

{
  "sample" : {}
}

When I assign $response['sample'] = array(), I get the following response

{
  "sample" : []
}

{}表示一个对象,因此您必须使用

$result = ["sample" => new stdclass()];

you must try JSON_FORCE_OBJECT as second argument while using json_encode function.

EDIT

$ar = array("sample" => array());
echo json_encode($ar, JSON_FORCE_OBJECT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM