[英]PHP Parse stdclass object
I get the following output (var_dump) from an API call. 我从API调用中获得以下输出(var_dump)。 I need to obtain the data elements or put them into an array so I can use them as individual values. 我需要获取数据元素或将它们放入数组,以便可以将它们用作单独的值。 I can't for the life of me work out how to do this. 我无法为自己的一生解决该怎么做。 For example, I need the value of <PaymentReference>
例如,我需要<PaymentReference>
的值
Can anyone assist? 有人可以协助吗?
Thanks 谢谢
object(stdClass)#2 (1) {
["GetPaymentsResult"]=>
object(stdClass)#3 (2) {
["schema"]=>
string(1754) "<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="Payments"><xs:element name="Payments" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Payment"><xs:complexType><xs:sequence><xs:element name="PaymentID" type="xs:string" minOccurs="0"/><xs:element name="EzidebitCustomerID" type="xs:int" minOccurs="0"/><xs:element name="YourSystemReference" type="xs:string" minOccurs="0"/><xs:element name="YourGeneralReference" type="xs:string" minOccurs="0"/><xs:element name="CustomerName" type="xs:string" minOccurs="0"/><xs:element name="DebitDate" type="xs:string" minOccurs="0"/><xs:element name="PaymentRef(erence" type="xs:string" minOccurs="0"/><xs:element name="PaymentMethod" type="xs:string" minOccurs="0"/><xs:element name="PaymentSource" type="xs:string" minOccurs="0"/><xs:element name="PaymentAmount" type="xs:decimal" minOccurs="0"/><xs:element name="SetupFee" type="xs:decimal" minOccurs="0"/><xs:element name="TransactionFeeClient" type="xs:decimal" minOccurs="0"/><xs:element name="TransactionFeeCustomer" type="xs:decimal" minOccurs="0"/><xs:element name="SettlementDate" type="xs:string" minOccurs="0"/><xs:element name="InvoiceID" type="xs:int" minOccurs="0"/><xs:element name="PaymentStatus" type="xs:string" minOccurs="0"/><xs:element name="BankReturnCode" type="xs:string" minOccurs="0"/><xs:element name="BankFailedReason" type="xs:string" minOccurs="0"/><xs:element name="BankReceiptID" type="xs:string" minOccurs="0"/><xs:element name="TransactionTime" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema>"
["any"]=>
string(6888) "<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><Payments xmlns=""><Payment diffgr:id="Payment1" msdata:rowOrder="0"><PaymentID>2012_41393</PaymentID><EzidebitCustomerID>385943</EzidebitCustomerID><YourSystemReference>365</YourSystemReference><YourGeneralReference>365</YourGeneralReference><CustomerName>365</CustomerName><DebitDate>2012-02-20</DebitDate><PaymentReference>10922</PaymentReference><PaymentMethod>DR</PaymentMethod><PaymentSource>SCHEDULED</PaymentSource><PaymentAmount>118.65</PaymentAmount><SetupFee>3.30</SetupFee><TransactionFeeClient>1.10</TransactionFeeClient><TransactionFeeCustomer>0.00</TransactionFeeCustomer>
Since it is a stdClass
object, you can access the values using object notation and you can use SimpleXML to parse the XML. 由于它是一个stdClass
对象,因此可以使用对象表示法访问值,也可以使用SimpleXML解析XML。 Here is an example: 这是一个例子:
$schema = $apiResult->GetPaymentsResult->schema;
$any = $apiResult->GetPaymentsResult->any;
$xml = new SimpleXmlElement($any);
echo $xml->Payments[0]->Payment->PaymentReference;
I'm not sure the PaymentReference
is correct, but if you see the SimpleXml examples you should be able to figure it out pretty easily. 我不确定PaymentReference
是否正确,但是如果您看到SimpleXml示例,则应该可以很容易地弄清楚它。
You can use regular expressions. 您可以使用正则表达式。 preg_match_all can help you. preg_match_all可以为您提供帮助。 For example: 例如:
function preg_xa($r,$co,$n=1){
$e=array();
preg_match_all($r,$co,$m);
for($i=0;$i<count($m[0]);$i++){
$e[]=$m[$n][$i];
}
return $e;
}
function get_PaymentReference($content){
return preg_xa( "/<PaymentReference>(.+)<\/PaymentReference>/simU", $content);
}
get_PaymentReference will return array of all values of PaymentReference tag. get_PaymentReference将返回PaymentReference标签的所有值的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.