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

时间:2017-10-09 07:40:25

标签: php arrays

我在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;

1 个答案:

答案 0 :(得分: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;