简体   繁体   English

PHP通过多维数组循环很慢

[英]php looping through multidimentional array is slow

I am working with the WooCommerce API to develop an small web application. 我正在使用WooCommerce API开发一个小型Web应用程序。 This application needs to pull in order information from wordpress/woocommerce. 该应用程序需要从wordpress / woocommerce中获取订单信息。

I am able to pull in some of this information fine, but having trouble with other bits (This is another question entirely.) 我可以很好地获取一些信息,但在其他方面遇到麻烦(这完全是另一个问题。)

The data form the woocommerce REST api is returned as a multidimensional array. 来自woocommerce REST api的数据以多维数组形式返回。

This is an example of a single order: 这是单个订单的示例:

    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)
              ...

Anywhere with a hash I have just removed the data for security reasons. 出于安全原因,我在任何带有哈希的地方都删除了数据。

So I need to loop through all the orders an output specific information. 所以我需要遍历所有订单的输出特定信息。 I can easily access strings within the main 'order' array by using this loop: 我可以使用以下循环轻松访问主“ order”数组中的字符串:

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

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


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

              }
            }

This loop runs and outputs the data in mere seconds. 该循环运行并在短短几秒钟内输出数据。

However when I come to outputting data from the 'line_items' array within the main order array: 但是,当我要从主订单数组中的“ line_items”数组输出数据时:

1) I cant seem to do it without specifying an order id otherwise I get an Undefined index: order and an invalid argument supplied for the line items foreach loop. 1)我似乎无法在未指定订单ID的情况下完成此操作,否则会得到未定义的索引:为订单项foreach循环提供的订单和无效参数。

to overcome that I have added this line to the main loop to get data form the 'line_items' array: 为了克服这一点,我将这一行添加到主循环中以从“ line_items”数组获取数据:

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

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

I then run a foreach over this to output the values form the 'line_items array' 然后,我对此运行一个foreach,以从“ line_items数组”输出值

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

             }

so this works, but outputting the 'line_items' this way makes the page load incredibly slow. 因此这可行,但是以这种方式输出'line_items'会使页面加载变得异常缓慢。 it takes about 40 seconds to loop through 6 orders. 完成6个订单大约需要40秒。

where as without this line: 如无此行:

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

The page renders in about 5 seconds. 页面将在大约5秒钟后呈现。

My question is: s this the best way to be accessing the 'line_items' array? 我的问题是:这是访问“ line_items”数组的最佳方法吗?

Below is the full code outputting the order information including the 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']';

                     }

              }
            }

Problem is that your top-level array has two elements, http and order , and the http section does NOT have order_number in it. 问题是您的顶级数组包含两个元素, httporder ,并且http部分中没有order_number

So basically you're doing 所以基本上你在做

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

You probably want a SINGLE loop: 您可能想要一个单循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM