php循环遍历multidimentional数组很慢

时间:2015-07-31 18:45:17

标签: php arrays wordpress loops

我正在使用WooCommerce API开发一个小型Web应用程序。此应用程序需要从wordpress / woocommerce获取信息。

我能够很好地提取这些信息,但是遇到其他问题(这完全是另一个问题。)

woocommerce REST api中的数据作为多维数组返回。

这是单个订单的示例:

    array (size=2)
  'order' => 
    array (size=30)
      'id' => int 22
      'order_number' => int 22
      'created_at' => string '2015-07-30T14:01:54Z' (length=20)
      'updated_at' => string '2015-07-30T14:01:54Z' (length=20)
      'completed_at' => string '2015-07-30T13:01:54Z' (length=20)
      'status' => string 'on-hold' (length=7)
      'currency' => string 'GBP' (length=3)
      'total' => string '3.84' (length=4)
      'subtotal' => string '3.84' (length=4)
      'total_line_items_quantity' => int 2
      'total_tax' => string '0.00' (length=4)
      'total_shipping' => string '0.00' (length=4)
      'cart_tax' => string '0.00' (length=4)
      'shipping_tax' => string '0.00' (length=4)
      'total_discount' => string '0.00' (length=4)
      'shipping_methods' => string '' (length=0)
      'payment_details' => 
        array (size=3)
          'method_id' => string 'bacs' (length=4)
          'method_title' => string 'Direct Bank Transfer' (length=20)
          'paid' => boolean false
      'billing_address' => 
        array (size=11)
          'first_name' => string 'Chris' (length=5)
          'last_name' => string '#' (length=5)
          'company' => string '' (length=0)
          'address_1' => string '#' (length=4)
          'address_2' => string '' (length=0)
          'city' => string '#' (length=7)
          'state' => string '' (length=0)
          'postcode' => string '#' (length=7)
          'country' => string 'GB' (length=2)
          'email' => string '#' (length=20)
          'phone' => string '#' (length=11)
      'shipping_address' => 
        array (size=9)
          'first_name' => string 'Chris' (length=5)
          'last_name' => string '#' (length=5)
          'company' => string '' (length=0)
          'address_1' => string '#' (length=4)
          'address_2' => string '' (length=0)
          'city' => string '#' (length=7)
          'state' => string '' (length=0)
          'postcode' => string '#' (length=7)
          'country' => string 'GB' (length=2)
      'note' => string '' (length=0)
      'customer_ip' => string '#' (length=15)
      '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)
      'customer_id' => int 1
      'view_order_url' => string '#' (length=58)
      'line_items' => 
        array (size=2)
          0 => 
            array (size=12)
              ...
          1 => 
            array (size=12)
              ...
      'shipping_lines' => 
        array (size=0)
          empty
      'tax_lines' => 
        array (size=0)
          empty
      'fee_lines' => 
        array (size=0)
          empty
      'coupon_lines' => 
        array (size=0)
          empty
      'customer' => 
        array (size=14)
          'id' => int 1
          'created_at' => string '2015-07-29T16:12:13Z' (length=20)
          'email' => string '#' (length=20)
          'first_name' => string '' (length=0)
          'last_name' => string '' (length=0)
          'username' => string '#' (length=6)
          'role' => string '#' (length=13)
          'last_order_id' => string '26' (length=2)
          'last_order_date' => string '2015-07-30T22:22:42Z' (length=20)
          'orders_count' => int 5
          'total_spent' => string '7.96' (length=4)
          'avatar_url' => string '#' (length=34)
          'billing_address' => 
            array (size=11)
              ...
          'shipping_address' => 
            array (size=9)
              ...
  'http' => 
    array (size=2)
      'request' => 
        array (size=7)
          'headers' => 
            array (size=3)
              ...
          'method' => string 'GET' (length=3)
          'url' => string '#' (length=290)
          'params' => 
            array (size=5)
              ...
          'data' => 
            array (size=0)
              ...
          'body' => null
          'duration' => float 5.01302
      'response' => 
        array (size=3)
          'body' => string '{"order":{"id":22,"order_number":22,"created_at":"2015-07-30T14:01:54Z","updated_at":"2015-07-30T14:01:54Z","completed_at":"2015-07-30T13:01:54Z","status":"on-hold","currency":"GBP","total":"3.84","subtotal":"3.84","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"bacs","method_title":"Direct Bank Transfer","paid":false},"billing_address":{"first_name":"Chris","last_na'... (length=2349)
          'code' => int 200
          'headers' => 
            array (size=5)
              ...

带有哈希的任何地方我出于安全原因删除了数据。

所以我需要遍历所有订单的输出特定信息。我可以轻松访问主要订单中的字符串'使用这个循环的数组:

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

            foreach( $orders as $order ) {
              foreach( $order as $value ) {


                    echo $value["order_number"];
                    $value["total"];

              }
            }

此循环仅在几秒钟内运行并输出数据。

然而,当我从' line_items'输出数据时主顺序数组中的数组:

1)我似乎无法在没有指定订单ID的情况下这样做,否则我会得到一个未定义的索引:订单和为行项目foreach循环提供的无效参数。

要克服这一点,我已将此行添加到主循环中,以便从“line_items”中获取数据。阵列:

$line_items = $connect->orders->get($value["order_number"]);

                        $line_items = $orders['order']['line_items'];

然后我运行一个foreach来输出' line_items数组中的值'

foreach($line_items as $item) {
                echo $item['name'];
                echo $item['quantity']';

             }

这样可行,但输出了' line_items'这种方式使页面加载速度极慢。循环完成6个订单大约需要40秒。

如果没有这一行:

$line_items = $connect->orders->get($value["order_number"]);

页面渲染时间约为5秒。

我的问题是:这是访问' line_items'的最佳方法。阵列?

以下是输出订单信息的完整代码,包括订单项。

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

            foreach( $orders as $order ) {
              foreach( $order as $value ) {


                    echo $value["order_number"];
                    $value["total"];

                    //get line items based on current order id - this is the slow bit!
                    $line_items = $connect->orders->get($value["order_number"]);
                    $line_items = $orders['order']['line_items'];

                    //loop through line items
                    foreach($line_items as $item) {
                        echo $item['name'];
                        echo $item['quantity']';

                     }

              }
            }

1 个答案:

答案 0 :(得分:0)

问题是您的顶级数组有两个元素httporder,而http部分中没有order_number

所以基本上你正在做

echo $array['order']['order_number']; // works
echo $array['http']['order_number'];  // doesn't work.

你可能想要一个SINGLE循环:

foreach($orders['order'] as $order)
   echo $order['order_number'];
}