<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); echo"<br/>"; var_dump(json_decode($json, true)); ?>
數組$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';被json_decode()解碼,轉換回來的數據是對象,var_dump(json_decode($json))得到的是一個對象,如下:
1
|
object(stdClass)#1 (5) { [
"a"
]=> int(1) [
"b"
]=> int(2) [
"c"
]=> int(3) [
"d"
]=> int(4) [
"e"
]=> int(5) }
|
那么,要怎么樣才能把json數組轉換為php格式的數組呢,采用以下方式:
json_decode($json, true)
這樣得到的數據就是php的數組了:
1
|
var_dump(json_decode(
$json
, true));
|
效果如下:
array(5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }