我似乎無法找到這個在線的答案,但我認爲這是因爲我不太確定要搜索什麼,因爲我對xml和json很陌生。json decode獲取屬性
如果我有這樣的XML
$postData = '<PaymentNotification Stage="false">
<MerchantReference>100004117</MerchantReference>
<TransactionType>PAYMENT</TransactionType>
<TransactionState>SUCCESSFUL</TransactionState>
<ResultCode>00</ResultCode>
<ResultMessage>Successful</ResultMessage>
<PayUReference>1167986976014</PayUReference>
<Basket>
<Description>Store Order Number:100004117</Description>
<AmountInCents>100</AmountInCents>
<CurrencyCode>ZAR</CurrencyCode>
<Products/>
</Basket>
<PaymentMethodsUsed>
<Eft BankName="ABSA" AmountInCents="100" Reference="CUMVSIUPFG" AccountNumber="4077920871" BranchNumber="632005" AccountType="Cheque" TimeLimit="168" Currency="ZAR"/>
</PaymentMethodsUsed>
<IpnExtraInfo>
<ResponseHash></ResponseHash>
</IpnExtraInfo>
</PaymentNotification>';
然後JSON解碼它
$returnData = json_decode(json_encode(simplexml_load_string($postData)),true);
我可以檢索的對象如。
$cost_amount = $returnData['Basket']['AmountInCents'];
但是,我如何獲得在EFT的屬性例如。銀行名?
我嘗試了這些沒有成功
$paid_amount = $returnData['PaymentMethodsUsed']['CreditCard']['@AmountInCents'];
and
$paid_amount = $returnData[0]->PaymentMethodsUsed[0]->CreditCard->AmountInCents;
這就是爲什麼有時這種方法不會削減它,它不是一個完美的方法來使用,因爲它不能完美地保存數據,你只需要走它正常的方式 – Ghost 2014-09-27 14:07:42
它也沒有那麼糟糕 - 旁邊有許多重複它現場 - 首先閱讀它在PHP手冊:[基本的SimpleXML用法](http://php.net/manual/en/ simplexml.examples-basic.php) – hakre 2014-09-28 08:38:59