2017-10-09 148 views
0

我正在爲一個foreach中的每個項目的變量內建一個數組。每個項目在變量'quantity'內都有一個名爲'quantity'的值。如果數量大於1那麼我需要創建許多數組。重複數組,如果變量大於1

我已經省略了很多代碼中的變量,使其更簡單,但一切都在這裏。如果數量大於1,那麼$ postData需要重複多次,所以我會爲每個數量獲取一個數組。

<?php 

foreach($order->get_items() as $item_key => $item_values): 

    $quantity = $item_data['quantity']; 


    if (has_term($settingsDigital, 'product_cat', $product_id)) { 

     $product_type = "VIRTUALCARD"; 
     $postData['bundles'][] = ['type' => $product_type, 'items' => [['bom' => [['type' => 'CARD', 'stockId' => $prod_sku, 'quantity' => $item_data['quantity'], 'metadata' => ['programme' => $settingsProgramme, 'denomination' => $item_data['total'], 'currency' => '826', 'mergeFields' => ['msg' => $giftMessage, 'giftAmount' => $formattedAmount, 'from' => $order_data['billing']['first_name'], 'to' => $theirName]], ]]]], 'delivery' => ['method' => 'EMAIL', 'recipientName' => $theirName, 'emailAddress' => $theirEmail]]; 

    } else if (has_term($settingsPhysical, 'product_cat', $product_id)) { 

     $product_type = "PICKANDPACK"; 
     $postData['bundles'][] = ['type' => $product_type, 'items' => [['bom' => [['type' => 'CARD', 'stockId' => $prod_sku, 'quantity' => $item_data['quantity'], 'metadata' => ['denomination' => $item_data['total'], 'currency' => '826']], ['type' => 'CARRIER', 'quantity' => 1, 'stockId' => $patt_carrier, 'metadata' => ['template' => $patt_template, 'mergeFields' => [['to' => '', 'msg' => '', 'giftAmount' => '', 'from' => '']]]], ['type' => "ENVELOPE", 'quantity' => 1, 'stockId' => 'ENV01']], ]], 'delivery' => ['method' => $shipping_method_name, 'shippingAddress' => ['firstname' => $fName, 'lastname' => $lName, 'addressLine1' => $addressLine1, 'addressLine2' => $addressLine2, 'town' => $town, 'county' => $county, 'postcode' => $postcode]]]; 

    } 

endforeach; 
+0

只是一個友好僅供參考,不最佳實踐速記循環,除非您選擇outputing成一個模板。 – DevDonkey

+1

謝謝你的建議,我會牢記這一點! –

回答

0

我不明白你爲什麼要存儲相同的數據quantity倍,但額外的循環應該有所幫助:

foreach($order->get_items() as $item_key => $item_values): 

    if (has_term($settingsDigital, 'product_cat', $product_id)) { 
     $product_type = "VIRTUALCARD"; 
     $product = ['type' => $product_type, 'items' => [['bom' => [['type' => 'CARD', 'stockId' => $prod_sku, 'quantity' => $item_data['quantity'], 'metadata' => ['programme' => $settingsProgramme, 'denomination' => $item_data['total'], 'currency' => '826', 'mergeFields' => ['msg' => $giftMessage, 'giftAmount' => $formattedAmount, 'from' => $order_data['billing']['first_name'], 'to' => $theirName]], ]]]], 'delivery' => ['method' => 'EMAIL', 'recipientName' => $theirName, 'emailAddress' => $theirEmail]]; 

    } else if (has_term($settingsPhysical, 'product_cat', $product_id)) { 
     $product_type = "PICKANDPACK"; 
     $product = ['type' => $product_type, 'items' => [['bom' => [['type' => 'CARD', 'stockId' => $prod_sku, 'quantity' => $item_data['quantity'], 'metadata' => ['denomination' => $item_data['total'], 'currency' => '826']], ['type' => 'CARRIER', 'quantity' => 1, 'stockId' => $patt_carrier, 'metadata' => ['template' => $patt_template, 'mergeFields' => [['to' => '', 'msg' => '', 'giftAmount' => '', 'from' => '']]]], ['type' => "ENVELOPE", 'quantity' => 1, 'stockId' => 'ENV01']], ]], 'delivery' => ['method' => $shipping_method_name, 'shippingAddress' => ['firstname' => $fName, 'lastname' => $lName, 'addressLine1' => $addressLine1, 'addressLine2' => $addressLine2, 'town' => $town, 'county' => $county, 'postcode' => $postcode]]]; 
    } 

    for ($i = 0; $i < $item_data['quantity']; $i++) { 
     $postData['bundles'][] = $product; 
    } 

endforeach; 
+0

不幸的是,從這個腳本獲取數據的API需要將數量設置爲1,然後重複實際數量的相同信息。謝謝你的回答,我會測試。 –