調用接口後返回JSON格式的數據
如果僅用json_decode($jsonArr);
object(stdClass)#9 (3) {
["code"] => string(3) "400"
["data"] => bool(false)
["message"] => string(24) "xxx"
}
生成的是一個對象,不能直接使用$arr[‘code’];
會報如下錯誤:
致命錯誤: Cannot use object of type stdClass as array
此時需要使用
json_decode($jsonArr,true);
當該參數爲 TRUE 時,將返回 array 而非 object 。
array(3) {
["code"] => string(3) "400"
["data"] => bool(false)
["message"] => string(24) "xxx"
}
此時我們就可以進行數組操作了。