php中如何將json轉(zhuǎn)換成對象數(shù)組?這篇文章運(yùn)用了實例代碼展示,代碼非常詳細(xì),可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。
在php中可以使用json_decode函數(shù)將json轉(zhuǎn)成對象數(shù)組。
json_decode是php5.2.0之后新增的一個PHP內(nèi)置函數(shù),其作用是對JSON格式的字符串進(jìn)行編碼.
那么這個函數(shù)該如何使用呢?
json_decode的語法規(guī)則:
json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
json_decode接受一個JSON格式的字符串并且把它轉(zhuǎn)換為PHP變量 ,當(dāng)該參數(shù)$assoc為TRUE時,將返回array,否則返回object。
JSON 格式的字符串
$json = '{"a":"php","b":"mysql","c":3}';
其中a為鍵,php為a的鍵值。
實例:
程序輸出:
stdClass Object ( [a] => php [b] => mysql [c] => 3 ) Array ( [a] => php [b] => mysql [c] => 3 )
在上面代碼的前提下
訪問對象類型$json_Class的a的值
echo $json_Class->{'a'};
程序輸出:php
訪問數(shù)組類型$json_Array的a的值
echo $json_Array['a'];
程序輸出:php
以上就是php中將json轉(zhuǎn)換成對象數(shù)組的具體操作,代碼詳細(xì)清楚,如果在日常工作遇到這個問題,希望你能通過這篇文章解決問題。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!