XML 数组 - 尝试获取非对象 foreach() 错误的属性 - 代码点火器


XML array - Trying to get property of non-object foreach() error - Codeigniter

我正在运行从xml数组中获取订单详细信息的scrip。 问题是每次脚本运行时我都会收到foreach()错误。我是标题中的html实体,但不确定。你能帮忙吗?

$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>

这应该有效,我用你的 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>' ; 
这对

我有用

            <?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;
            ?>