PHP 中眠屎,不管是 list 或者 dictionary 都使用一樣的 [](或者 array()) 來(lái)定義。
在使用 JSON 作為 API 數(shù)據(jù) Content-Type 的時(shí)候耗溜,會(huì)有這樣一個(gè)問(wèn)題:
如何返回一個(gè)空對(duì)象和一個(gè)空數(shù)組组力?
使用:json_encode([]),得到JSON結(jié)果:[]
使用:json_encode(new stdClass)抖拴,得到JSON結(jié)果:{}
stdClass 在 PHP 中是一個(gè)類燎字,不能像數(shù)組一樣使用,但可以用下面的辦法賦值:
$a = 'attrName';
$class = new stdClass();
$class->{$a} = 1111;