简体   繁体   中英

Repeat array if variable is greater than 1

I am building an array within a variable for each item within a foreach. Each item has a value called 'quantity' within the variable 'quantity' . If the quantity is greater than 1 then I need to create that many arrays.

I have omitted a lot of the variables within the code to make it simpler but everything is here. If quantity is greater than 1 then the $postData needs to be repeated that many times so I would get an array for each quantity.

<?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;

I don't understand why you should store the same data for quantity times, but additional loop should help:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM