PHP解析stdclass对象

时间:2012-03-22 06:05:48

标签: php xml

我从API调用中获得以下输出(var_dump)。我需要获取数据元素或将它们放入数组中,以便将它们用作单个值。我不能为我的生活弄清楚如何做到这一点。例如,我需要<PaymentReference>

的值

有人可以帮忙吗?

由于

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>

2 个答案:

答案 0 :(得分:0)

由于它是stdClass对象,您可以使用对象表示法访问值,并且可以使用SimpleXML来解析XML。这是一个例子:

$schema = $apiResult->GetPaymentsResult->schema;
$any    = $apiResult->GetPaymentsResult->any;

$xml    = new SimpleXmlElement($any);

echo $xml->Payments[0]->Payment->PaymentReference;

我不确定PaymentReference是否正确,但如果您看到SimpleXml示例,您应该能够很容易地找到它。

答案 1 :(得分:0)

您可以使用正则表达式。 preg_match_all可以帮到你。例如:

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将返回PaymentReference标记的所有值的数组。