我需要能够读取此响应数组中apiaccountview_id的数量:
{
"errorCodes": null,
"result": {
"apiaccountview_id": "553631",
"apiaccountview_countryId": "UA",
"apiaccountview_registrationIp": "531137938",
"apiaccountview_phoneNumber": "12344512344"
},
"success": true
}
我正在使用
$obj = json_decode($response,true);
我希望这能显示 apiaccountview_id
if ($obj['success'] =='true'){
return array(
'error' => 0,
'msg' => "added successfully - " . $obj['result=>apiaccountview_id'] . ":" . $obj['result=>apiaccountview_id']
);
}
else{
return array(
'error' => 1,
'msg' => "Error in loading - " . $obj['errorCodes'] . ":" . $obj['result']
);
}
因为结果是一个数组,我正在考虑像这样调用里面的值:
$obj['result=>apiaccountview_id']
但它没有用。 你能帮我理解为什么吗? 如何在json数组中获取值?
谢谢你, 阿里耶
答案 0 :(得分:3)
您将true
作为第二个参数传递给json_decode
,这意味着您的结果中将包含关联数组。您可以像这样访问apiaccountview_id
:
echo $obj['result']['apiaccountview_id'];
如果你想返回对象而不是数组,你应该传递false
(这是默认值)然后你可以像这样访问属性:
echo $obj->result->apiaccountview_id;