首頁 > 後端開發 > php教程 > json_encode json_force_object问题

json_encode json_force_object问题

WBOY
發布: 2016-06-06 20:25:03
原創
1315 人瀏覽過

<code><?php var_dump(json_encode(array("Apple", "Banana", "Pear"))); 
//string(25) "["Apple","Banana","Pear"]"

var_dump(json_encode((object)array("Apple", "Banana", "Pear")));
//string(37) "{"0":"Apple","1":"Banana","2":"Pear"}"

var_dump(json_encode(array("Apple", "Banana", "Pear"),JSON_FORCE_OBJECT));
//string(37) "{"0":"Apple","1":"Banana","2":"Pear"}"

var_dump(json_encode(array(0=>"APPLE",1=>"BABABA")),JSON_FORCE_OBJECT);

//string(18) "["APPLE","BABABA"]"


</code>
登入後複製
登入後複製

最后两行:

<code>var_dump(json_encode((object)array(0=>"APPLE",1=>"BABABA",2=>'PEAR')));
// string(37) "{"0":"APPLE","1":"BABABA","2":"PEAR"}"
var_dump(json_encode(array(0=>"APPLE",1=>"BABABA",2=>'PEAR')),JSON_FORCE_OBJECT);
string(25) "["APPLE","BABABA","PEAR"]"
int(16)</code>
登入後複製
登入後複製

int (16) 是个啥??加了json_force_object也不好使呢,如果下标不以0开头,或者key不连续,就是object,否则就是数组,哪位前辈给大概解释下为啥,为啥。

回复内容:

<code><?php var_dump(json_encode(array("Apple", "Banana", "Pear"))); 
//string(25) "["Apple","Banana","Pear"]"

var_dump(json_encode((object)array("Apple", "Banana", "Pear")));
//string(37) "{"0":"Apple","1":"Banana","2":"Pear"}"

var_dump(json_encode(array("Apple", "Banana", "Pear"),JSON_FORCE_OBJECT));
//string(37) "{"0":"Apple","1":"Banana","2":"Pear"}"

var_dump(json_encode(array(0=>"APPLE",1=>"BABABA")),JSON_FORCE_OBJECT);

//string(18) "["APPLE","BABABA"]"


</code>
登入後複製
登入後複製

最后两行:

<code>var_dump(json_encode((object)array(0=>"APPLE",1=>"BABABA",2=>'PEAR')));
// string(37) "{"0":"APPLE","1":"BABABA","2":"PEAR"}"
var_dump(json_encode(array(0=>"APPLE",1=>"BABABA",2=>'PEAR')),JSON_FORCE_OBJECT);
string(25) "["APPLE","BABABA","PEAR"]"
int(16)</code>
登入後複製
登入後複製

int (16) 是个啥??加了json_force_object也不好使呢,如果下标不以0开头,或者key不连续,就是object,否则就是数组,哪位前辈给大概解释下为啥,为啥。

请仔细对比以下两行

<code>var_dump(json_encode(array(0=>"APPLE",1=>"BABABA",2=>'PEAR')),JSON_FORCE_OBJECT);
var_dump(json_encode(array(0=>"APPLE",1=>"BABABA",2=>'PEAR'),JSON_FORCE_OBJECT));</code>
登入後複製
相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板