[英]Access part of SimpleXMLElement Object - PHP
I need to loop through the items
as an array within the SimpleXMLElement Object below but cannot seem to access it using $order->order->order->items
. 我需要在下面的SimpleXMLElement对象中循环遍历
items
,但似乎无法使用$order->order->order->items
访问它。 I can access the delivery and billing addresses using the same format, ie. 我可以使用相同的格式访问递送和帐单地址,即。
$order->order->order->delivery_address
and expected to get to the items array in the same way. $order->order->order->delivery_address
并期望以相同的方式到达items数组。 However, I get an empty SimpleXMLElement Object when I print_r($order->order->order->items)
但是,当我
print_r($order->order->order->items)
时,我得到一个空的SimpleXMLElement对象
SimpleXMLElement Object
(
[order] => SimpleXMLElement Object
(
[id] => 860268
[shopkeeper_orderno] => 1001
[customer] => 797476
[creationdate] => Apr 19 2012 10:36:38:100AM
[reference] => k2koju45rmaqfl45n20xbkmq
[net] => 1500
[vat] => 17.5
[status] => 0
[isnew] => 1
[deductions] => 0
[postage] => 1
[paymentmethod] => PayPal
[instructions] => SimpleXMLElement Object
(
)
[errors] => SimpleXMLElement Object
(
)
[kashflow_synch] => 0
[order] => Array
(
[0] => SimpleXMLElement Object
(
[billing_address] => SimpleXMLElement Object
(
[0] =>
)
)
[1] => SimpleXMLElement Object
(
[delivery_address] => SimpleXMLElement Object
(
[0] =>
)
)
[2] => SimpleXMLElement Object
(
[items] => Array
(
[0] => SimpleXMLElement Object
(
[id] => 1285158
[headerID] => 860268
[productID] => 4867690
[description] => TEST ORDERING PF NODES - Special Offer Price
[net] => 1400
[vat] => 0
[qty] => 1
[formID] => -1
)
[1] => SimpleXMLElement Object
(
[id] => 1285159
[headerID] => 860268
[productID] => 4959678
[description] => Wedding dress
[net] => 100
[vat] => 17.5
[qty] => 1
[formID] => -1
)
)
)
)
[postage_tax] => 0
[dispatched] => 0
[paybyotherid] => -1
[ip] => 81.168.43.121
[wheredidyouhearid] => -1
)
)
EDIT : I just saw you made a mistake with the naming, the parent needs to be called <orders>
and the sub items <order>
编辑 :我刚看到你的命名错误,父母需要被称为
<orders>
和子项<order>
The SimpleXMLElement seems to be empty, in fact it's usually filled but not displayed when dumping (whoever thought of this crazy behaviour) SimpleXMLElement 似乎是空的,实际上它通常是填充但在转储时不显示(谁想到这个疯狂的行为)
Can you try this? 你能试试吗?
foreach($order->orders->order as $order) { // should be orders then
echo $item->getName();
}
Or try it with SimpleXMLElement::children() 或者尝试使用SimpleXMLElement :: children()
your items are actually on the second offset of the order array. 您的商品实际上是订单数组的第二个偏移量。
I'd just use the xPath to process these. 我只是使用xPath来处理这些。
foreach($xmlObject->xpath('/order/order[2]/items') as $item)
{
// Do something with my $item
}
You can use a loop like the one below, then all you need to do is $items->id 您可以使用如下所示的循环,然后您需要做的就是$ items-> id
foreach($order->children()->children()->items as $items)
{
}
Using Dan Lees suggestion I tried SimpleXMLElement::children() and did the below which works 使用Dan Lees的建议我尝试使用SimpleXMLElement :: children()并执行以下操作
foreach ($order->children() as $order) {
foreach ($order->children() as $order_details) {
foreach ($order_details->children() as $order_items) {
echo $order_items->id;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.