请原谅我提出这样一个“noob”问题,但我一直试图找到一个简单的例子,说明过去几个小时如何遍历我返回的xml,我没有在哪里。我只想循环遍历xml并为每个订单提取'Amount'属性和'AmazonOrderId'属性。我不知道如何循环,也不知道如何获取相关数据。
SimpleXMLElement Object
(
[ListOrdersResult] => SimpleXMLElement Object
(
[Orders] => SimpleXMLElement Object
(
[Order] => Array
(
[0] => SimpleXMLElement Object
(
[ShipmentServiceLevelCategory] => SecondDay
[OrderTotal] => SimpleXMLElement Object
(
[Amount] => 5.93
[CurrencyCode] => USD
)
[SellerOrderId] => 107-1261608-7067458
[FulfillmentChannel] => AFN
[BuyerEmail] => 5qhs64ktb88pdsj@marketplace.amazon.com
[OrderStatus] => Shipped
[BuyerName] => Derrick D Vann
[ShipServiceLevel] => SecondDay
[LastUpdateDate] => 2013-03-13T02:20:31Z
[PurchaseDate] => 2013-03-11T06:14:40Z
[NumberOfItemsUnshipped] => 0
[MarketplaceId] => ATVPDKIKX0DER
[SalesChannel] => Amazon.com
[ShippingAddress] => SimpleXMLElement Object
(
[Phone] => 202 746-2567
[PostalCode] => 20001-4040
[Name] => Derrick Vann
[CountryCode] => US
[StateOrRegion] => DC
[AddressLine1] => 2120 Vermont Ave NW Apt 117
[City] => Washington
)
[NumberOfItemsShipped] => 1
[AmazonOrderId] => 107-1261608-7067458
[PaymentMethod] => Other
)
[1] => SimpleXMLElement Object
(
[ShipmentServiceLevelCategory] => Expedited
[OrderTotal] => SimpleXMLElement Object
(
[Amount] => 23.30
[CurrencyCode] => USD
)
[SellerOrderId] => 104-9066827-4446667
[FulfillmentChannel] => AFN
[BuyerEmail] => 6kfc88nrsnm83fq@marketplace.amazon.com
[OrderStatus] => Shipped
[BuyerName] => Quoc Bui
[ShipServiceLevel] => Expedited
[LastUpdateDate] => 2013-03-13T09:34:26Z
[PurchaseDate] => 2013-03-11T08:07:13Z
[NumberOfItemsUnshipped] => 0
[MarketplaceId] => ATVPDKIKX0DER
[SalesChannel] => Amazon.com
[ShippingAddress] => SimpleXMLElement Object
(
[Phone] => (02) 9560 3639
[PostalCode] => 2204
[Name] => Quoc Minh Bui
[CountryCode] => AU
[StateOrRegion] => New South Wales
[AddressLine1] => 19 Centennial St
[City] => Marrickville
)
[NumberOfItemsShipped] => 1
[AmazonOrderId] => 104-9066827-4446667
[PaymentMethod] => Other
)
...
答案 0 :(得分:3)
SimpleXML很棒,不是吗?
foreach ($xml->ListOrdersResult->Orders->Order as $order) {
$amazonOrderId = (string) $order->AmazonOrderId;
$orderTotal = (string) $order->OrderTotal->Amount;
}
我使用(string)
类型转换,因为否则会为这些标量值返回SimpleXMLElement
。
答案 1 :(得分:0)
为每个订单提取'Amount'属性和'AmazonOrderId'属性。
如果我一字一句地带你,那么这就是你要找的东西:
$allThoseAttributes = $xml->xpath('//Order/@Amount|//Order/@AmazonOrderId');
但是,您更有可能在寻找:
$orders = [];
foreach ($xml->xpath('//Order') as $order) {
$orders[] = [
'amazon' => (string) $order->AmazonOrderId
'total' => (string) $order->OrderTotal->Amount;
];
}
因为你所谓的属性是因果元素。如果您想了解有关XML和Xpath的更多信息,请阅读以下内容:XPath