如果变量大于 1,则重复数组



我正在为foreach中的每个项目在变量中构建一个数组。每个项目在变量 'quantity' 中都有一个名为 'quantity' 的值。如果数量大于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;
我不

明白为什么要将相同的数据存储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;

最新更新