Php XML数组-尝试获取非对象foreach()的属性错误-Codeigniter
我正在运行从xml数组中获取订单详细信息的scrip。问题是每次脚本运行时都会出现foreach()错误。我很高兴这是一个在标题上的问题,但不确定。你能帮忙吗Php XML数组-尝试获取非对象foreach()的属性错误-Codeigniter,php,arrays,xml,codeigniter,foreach,Php,Arrays,Xml,Codeigniter,Foreach,我正在运行从xml数组中获取订单详细信息的scrip。问题是每次脚本运行时都会出现foreach()错误。我很高兴这是一个在标题上的问题,但不确定。你能帮忙吗 $products=''; $xml=simplexml_load_string($response); $temp_arr=array(); foreach($xml->ListOrderItemsResult->OrderItems->OrderItem as $product) { $temp_
$products='';
$xml=simplexml_load_string($response);
$temp_arr=array();
foreach($xml->ListOrderItemsResult->OrderItems->OrderItem as $product)
{
$temp_arr[]=array('item'=>(string)$product->Title,'quantity'=>(string)$product->QuantityOrdered,'order_item_code'=>(string)$product->OrderItemId,'sku'=>(string)$product->SellerSKU);
}
return $temp_arr;
}
数组是否正确我正在检查数组中的所有数据是否存在空值。
我在同一行中遇到2个错误-foreach($xml->ListOrderItemsResult->OrderItems->OrderItems as$product)
正在尝试获取非对象的属性
为foreach()提供的参数无效
XML
32080774637267
0
英镑
1.
0
英镑
KS-MQOX-EUQ5
单双;特大号床笠枕套定制床上用品(灰色,枕套标准19英寸x29)
0
英镑
2.95
英镑
0
英镑
2.99
英镑
0
英镑
刚出现的
B00AFB1XH8
0
刚出现的
全新的
0
英镑
026-3622751-4319550
42f09535-70e7-4a28-8413-cc439c42c030
我用你的xml测试了一下,这应该行得通
foreach ($xml->ListOrderItemsResult->OrderItems as $items) {
foreach( $items->OrderItem as $orderitem ){
echo 'test id is' . $orderitem->OrderItemId ;
}
}
可以有多个订单项目组,每组可以包含多个项目。您也可以这样做来进行测试
echo $xml->ListOrderItemsResult->OrderItems->OrderItem[0]->OrderItemId ;
并始终检查xml结构,如
echo '<pre>' ;
print_r($xml);
echo '</pre>' ;
echo';
<?php
$xml = '<?xml version="1.0"?>
<ListOrderItemsResponse xmlns="https://mws.amazonservices.com/Orders/2011-01-01">
<ListOrderItemsResult>
<OrderItems>
<OrderItem>
<OrderItemId>32080774637267</OrderItemId>
<GiftWrapPrice>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</GiftWrapPrice>
<QuantityOrdered>1</QuantityOrdered>
<GiftWrapTax>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</GiftWrapTax>
<SellerSKU>KS-MQOX-EUQ5</SellerSKU>
<Title>SINGLE DOUBLE & KING SIZE FITTED SHEETS Pillow cases BESPOKE BEDDING (Grey, pillowcases Standard 19"x29")</Title>
<ShippingTax>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ShippingTax>
<ShippingPrice>
<Amount>2.95</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ShippingPrice>
<ItemTax>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ItemTax>
<ItemPrice>
<Amount>2.99</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ItemPrice>
<PromotionDiscount>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</PromotionDiscount>
<ConditionId>New</ConditionId>
<ASIN>B00AFB1XH8</ASIN>
<QuantityShipped>0</QuantityShipped>
<ConditionSubtypeId>New</ConditionSubtypeId>
<ConditionNote>Brand New</ConditionNote>
<ShippingDiscount>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ShippingDiscount>
</OrderItem>
</OrderItems>
<AmazonOrderId>026-3622751-4319550</AmazonOrderId>
</ListOrderItemsResult>
<ResponseMetadata>
<RequestId>42f09535-70e7-4a28-8413-cc439c42c030</RequestId>
</ResponseMetadata>
</ListOrderItemsResponse>';
$list = new SimpleXMLElement($xml);
echo $list->ListOrderItemsResult->OrderItems->OrderItem->OrderItemId;
?>
打印(xml);
回声';
这对我很有用
有人能帮我吗?如果你能提供实际的XML,或者至少是其中的一部分,那就太好了。更新。。请检查我现在只收到“为foreach()提供的参数无效”错误。您是否尝试了第二个示例?在执行foreach()之前——尝试回显一些简单的值,以确保其他所有内容都正常工作。
<?php
$xml = '<?xml version="1.0"?>
<ListOrderItemsResponse xmlns="https://mws.amazonservices.com/Orders/2011-01-01">
<ListOrderItemsResult>
<OrderItems>
<OrderItem>
<OrderItemId>32080774637267</OrderItemId>
<GiftWrapPrice>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</GiftWrapPrice>
<QuantityOrdered>1</QuantityOrdered>
<GiftWrapTax>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</GiftWrapTax>
<SellerSKU>KS-MQOX-EUQ5</SellerSKU>
<Title>SINGLE DOUBLE & KING SIZE FITTED SHEETS Pillow cases BESPOKE BEDDING (Grey, pillowcases Standard 19"x29")</Title>
<ShippingTax>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ShippingTax>
<ShippingPrice>
<Amount>2.95</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ShippingPrice>
<ItemTax>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ItemTax>
<ItemPrice>
<Amount>2.99</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ItemPrice>
<PromotionDiscount>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</PromotionDiscount>
<ConditionId>New</ConditionId>
<ASIN>B00AFB1XH8</ASIN>
<QuantityShipped>0</QuantityShipped>
<ConditionSubtypeId>New</ConditionSubtypeId>
<ConditionNote>Brand New</ConditionNote>
<ShippingDiscount>
<Amount>0.00</Amount>
<CurrencyCode>GBP</CurrencyCode>
</ShippingDiscount>
</OrderItem>
</OrderItems>
<AmazonOrderId>026-3622751-4319550</AmazonOrderId>
</ListOrderItemsResult>
<ResponseMetadata>
<RequestId>42f09535-70e7-4a28-8413-cc439c42c030</RequestId>
</ResponseMetadata>
</ListOrderItemsResponse>';
$list = new SimpleXMLElement($xml);
echo $list->ListOrderItemsResult->OrderItems->OrderItem->OrderItemId;
?>