[英]How to parse stdClass object?
I have a SOAP response whose var_dump looks like this: 我有一个SOAP响应,其var_dump如下所示:
object(stdClass)[14]
public 'GetClientsResult' =>
object(stdClass)[15]
I can't figure out how to parse this for the life of me, I've never used stdClass before. 我无法弄清楚如何解析它,因为我从来没有使用过stdClass。
How can I parse this response in PHP? 如何在PHP中解析此响应?
For starters, you can cast it into an array (assuming the object is stored in $response
): 对于初学者,您可以将其转换为数组(假设对象存储在
$response
):
$response = (array) $response;
Or you can access things by: 或者您可以通过以下方式访问内容:
$response->GetClientResult->otherStuff;
An StdClass
is an empty class where you can set and get property values. StdClass
是一个空类,您可以在其中设置和获取属性值。 An example: 一个例子:
<?php
// $response is a normal array
$response['GetClientResult'] = 'foo'; // set
$response['GetClientResult']; // get
// $response is a StdClass
$response->GetClientResult = 'foo'; // set
$response->GetClientResult; // get
?>
And if you want to cast the class back to an array you can use: 如果要将类强制转换回数组,则可以使用:
$response = (array) $response
And if you want to do that recursive, because you have multiple StdClasses: 如果要进行递归操作,因为您有多个StdClasses:
function StdClass2array(StdClass $class)
{
$array = array();
foreach ($class as $key => $item)
{
if ($item instanceof StdClass) {
$array[$key] = StdClass2array($item);
} else {
$array[$key] = $item;
}
}
return $array;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.