【问题标题】:XML array - Trying to get property of non-object foreach() error - CodeigniterXML 数组 - 试图获取非对象 foreach() 错误的属性 - Codeigniter
【发布时间】:2015-03-28 18:47:05
【问题描述】:

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

$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,或者至少是其中的一部分,那就太好了。
  • 已更新..请检查

标签: php arrays xml codeigniter foreach


【解决方案1】:

这应该可以工作,我用你的 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>' ; 

【讨论】:

  • 现在我只收到“为 foreach() 提供的参数无效”错误。
  • 你试过第二个例子吗?在执行 foreach() 之前 - 尝试回显一些更简单的值作为检查以确保其他一切正常。
【解决方案2】:

这对我有用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2015-09-22
    • 2021-03-27
    • 2011-07-07
    相关资源
    最近更新 更多