简体   繁体   中英

php looping through multidimentional array is slow

I am working with the WooCommerce API to develop an small web application. This application needs to pull in order information from 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.

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:

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

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.

to overcome that I have added this line to the main loop to get data form the 'line_items' array:

$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 as $item) {
                echo $item['name'];
                echo $item['quantity']';

             }

so this works, but outputting the 'line_items' this way makes the page load incredibly slow. it takes about 40 seconds to loop through 6 orders.

where as without this line:

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

The page renders in about 5 seconds.

My question is: s this the best way to be accessing the 'line_items' array?

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.

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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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