2017-10-09 54 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,然后重复实际数量的相同信息。谢谢你的回答,我会测试。 –