php – 如何获取数组中对象元素的值
如下所示,var_dump出来的结果,我想取出error的内容array(2) {
=> NULL
=> object(Qiniu\Http\Error)#37 (2) {
["url":"Qiniu\Http\Error":private] => string(71) "http://rs.qiniu.com/delete/dGVzdHlqczphcmNoaXRlY3R1cmUtMTQ3NzA5OC5qcGc="
["response":"Qiniu\Http\Error":private] => object(Qiniu\Http\Response)#38 (6) {
["statusCode"] => int(612)
["headers"] => array(8) {
["Server"] => string(9) "openresty"
["Date"] => string(19) "Sat, 03 Oct 2020 01"
["Content-Type"] => string(16) "application/json"
["Content-Length"] => string(2) "37"
["Connection"] => string(10) "keep-alive"
["Cache-Control"] => string(8) "no-store"
["X-Reqid"] => string(16) "0cQAAABpWCooVjoW"
["X-Log"] => string(1) "-"
}
["body"] => string(37) "{"error":"no such file or directory"}"
["error"] => string(25) "no such file or directory"
["jsonData":"Qiniu\Http\Response":private] => array(1) {
["error"] => string(25) "no such file or directory"
}
["duration"] => float(0.074)
}
}
}
判断是否存在 然后 ->error; object转array函数 因为 error 在 Qiniu\Http\Error 类的私有属性response 上,你的看该类是否有 __get()这样的方法 是否能获取到 response 这个属性,然后你就可以 $arr->response ->error;
如果是有类似 getResponse 的方法来获取response 属性就 $arr->getResponse ()->error; //使用json_decode()函数
$json = json_decode( $str, 1 );//$str代表你上面那段的字符串内容
$data=$json['1']['response'];
$error=$data['error'];
echo $error;
页:
[1]