簡體   English   中英

PHP通過多維數組循環很慢

[英]php looping through multidimentional array is slow

我正在使用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)
              ...

出於安全原因,我在任何帶有哈希的地方都刪除了數據。

所以我需要遍歷所有訂單的輸出特定信息。 我可以使用以下循環輕松訪問主“ order”數組中的字符串:

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

                     }

              }
            }

問題是您的頂級數組包含兩個元素, httporder ,並且http部分中沒有order_number

所以基本上你在做

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

您可能想要一個單循環:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM