【问题标题】:php looping through stdClass objects and arraysphp循环遍历stdClass对象和数组
【发布时间】:2015-10-24 02:43:11
【问题描述】:

您好,我使用 woocommerce rest API 访问 woocommerce 网站上的订单信息。

数据以 json 格式返回,我可以通过这个循环访问所有数据:

$orders = $connect->orders->get();
        $order_numbers = $orders->orders;

        foreach ($order_numbers as $order_number) {            

            $order_data = $connect->orders->get($order_number->order_number);

            echo '</br>' . $order_number->order_number . '</br>';
            echo $order_data->order->total . '</br>';

            $line_items = $order_data->order->line_items;

            foreach($line_items as $item){
                echo $item->name . ' x ' . $item->quantity . '</br>';   
            }

        }

第一行是获取订单列表的 API 调用。

这允许我在不指定 order_number 的情况下从第一个对象输出数据。但是,当访问 stdObject 中的数组时,例如“line_items”,我必须定义一个订单号。为了克服这个问题,我这次在 foreach 内部进行了另一个 api 调用,指定了访问信息的订单号。

$order_data = $connect->orders->get($order_number->order_number);

问题在于第二个 API 调用大大减慢了整个页面的速度。

有没有办法让我将其减少为单个 api 调用以加快页面渲染速度。

作为参考,这是我正在访问的数据:

object(stdClass)[15]
  public 'order' => 
    object(stdClass)[16]
      public 'id' => int 22
      public 'order_number' => int 22
      public 'created_at' => string '2015-07-30T14:01:54Z' (length=20)
      public 'updated_at' => string '2015-07-30T14:01:54Z' (length=20)
      public 'completed_at' => string '2015-07-30T13:01:54Z' (length=20)
      public 'status' => string 'on-hold' (length=7)
      public 'currency' => string 'GBP' (length=3)
      public 'total' => string '3.84' (length=4)
      public 'subtotal' => string '3.84' (length=4)
      public 'total_line_items_quantity' => int 2
      public 'total_tax' => string '0.00' (length=4)
      public 'total_shipping' => string '0.00' (length=4)
      public 'cart_tax' => string '0.00' (length=4)
      public 'shipping_tax' => string '0.00' (length=4)
      public 'total_discount' => string '0.00' (length=4)
      public 'shipping_methods' => string '' (length=0)
      public 'payment_details' => 
        object(stdClass)[17]
          public 'method_id' => string 'bacs' (length=4)
          public 'method_title' => string 'Direct Bank Transfer' (length=20)
          public 'paid' => boolean false
      public 'billing_address' => 
        object(stdClass)[18]
          public 'first_name' => string 'Chris' (length=5)
          public 'last_name' => string '#' (length=5)
          public 'company' => string '' (length=0)
          public 'address_1' => string '#' (length=4)
          public 'address_2' => string '' (length=0)
          public 'city' => string '#' (length=7)
          public 'state' => string '' (length=0)
          public 'postcode' => string '#' (length=7)
          public 'country' => string 'GB' (length=2)
          public 'email' => string '#' (length=20)
          public 'phone' => string '#' (length=11)
      public 'shipping_address' => 
        object(stdClass)[19]
          public 'first_name' => string 'Chris' (length=5)
          public 'last_name' => string '#' (length=5)
          public 'company' => string '' (length=0)
          public 'address_1' => string '#' (length=4)
          public 'address_2' => string '' (length=0)
          public 'city' => string '#' (length=7)
          public 'state' => string '' (length=0)
          public 'postcode' => string '#' (length=7)
          public 'country' => string 'GB' (length=2)
      public 'note' => string '' (length=0)
      public 'customer_ip' => string '#' (length=15)
      public 'customer_user_agent' => string 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4' (length=134)
      public 'customer_id' => int 1
      public 'view_order_url' => string '#' (length=58)
      public 'line_items' => 
        array (size=2)
          0 => 
            object(stdClass)[20]
              ...
          1 => 
            object(stdClass)[21]
              ...
      public 'shipping_lines' => 
        array (size=0)
          empty

【问题讨论】:

    标签: php arrays json stdclass


    【解决方案1】:

    根据他们的规范 (http://woothemes.github.io/woocommerce-rest-api-docs/#view-list-of-orders),您应该已经拥有 $orders 结果中的所有信息。

    【讨论】:

    • 我愿意,但问题是在访问 line_items 数组时,我必须指定一个订单 ID,这只能通过我所看到的 API 调用来完成。如果我没有指定一个 ID,我会得到一个 php 无效索引错误。并且指定 ID 会破坏循环遍历所有订单的对象。
    • 据我所知,视图列表订单和视图订单的订单结构,第一个只是订单对象数组。你为什么不试试 foreach ($order_numbers as $order) { echo '' 。 $order->order_number 。 '';回声 $order->total 。 ''; $line_items = $order->line_items; foreach($line_items as $item){ echo $item->name . ' X ' 。 $项目->数量。 ''; } }
    • 非常感谢。这很完美。我现在可以看到比较我的代码和你的代码我做错了什么。页面现在立即呈现!
    猜你喜欢
    • 2014-06-18
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多