XML数组 - 试图获取非对象foreach()错误的属性 - Codeigniter

时间:2015-01-29 13:54:38

标签: php arrays xml codeigniter foreach

我正在运行脚本,它从xml数组中获取订单详细信息。问题是每次脚本运行时我都会遇到foreach()错误。我认为这是标题中的不足之处,但不确定。能帮到你吗?

$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->OrderItem as $product)

  

尝试获取非对象的属性

     

为foreach()提供的参数无效

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 &amp; KING SIZE FITTED SHEETS Pillow cases BESPOKE BEDDING (Grey, pillowcases Standard 19&quot;x29&quot;)</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>

2 个答案:

答案 0 :(得分:0)

这应该适用于我用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>' ; 

答案 1 :(得分:0)

这对我有用

            <?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 &amp; KING SIZE FITTED SHEETS Pillow cases BESPOKE BEDDING (Grey, pillowcases Standard 19&quot;x29&quot;)</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;
            ?>