如何回显嵌套的 stdClass PHP(json 编码解码)
How to echo nested stdClass PHP (json encode decode)
我有以下标准类对象数组:
var_dump($order);
array (size=2)
'raw' =>
object(stdClass)[6]
public 'actionCodeDescription' => string 'processing.error.203' (length=20)
public 'params' =>
object(stdClass)[7]
public 'respCode_desc' => string 'Transaction rejected' (length=83)
我正在尝试检索并显示“params' {'respCode_desc : "transact rejected'}
例如如果我要求
actionCodeDescription
我只需要echo $order->message();
public function message()
{
return $this->raw->actionCodeDescription;
}
我试过的方法:$this->raw->params['0']->respCode_desc;
没用
我会得到
Trying to get property 'respCode_desc' of non-object or
Trying to access array offset on value of type null
知道我做错了什么
还有一件事我如何检查 respCode_desc
是否不为空以便我可以显示
actionCodeDescription
?
这是我的代码
class Order
{
public $raw;
function __construct($obj)
{
$this->raw = $obj;
}
public function __toString()
{
return json_encode($this->raw, JSON_PRETTY_PRINT);
}
已解决
<?php echo $order->getCibMessage(); ?>
function getCibMessage()
{
$obj = $this->raw;
if (isset($obj->params) && isset($obj->params->respCode_desc)) {
return $obj->params->respCode_desc;
} else {
return $obj->actionCodeDescription;
}
}