1

how to create a dynamic array? I need to set the dynamic value of product id and qty and passing into the items array.

$itemarray = [];
foreach ($ItemCollection as $item) {
    $productId = $item['order_item_id'];
    $qty = $item['qty'];
}  
        $orderData = [
            'email'        => $customerEmail, //buyer email id
            'shipping_address' => [
            'firstname'    => $firstname, //address Details
            'lastname'     => $lastname,
            'street' => $address,
            'city' => $city,
            'country_id' => $countryid,
            'region' => $region,
            'regionId' => $regionid,
            'postcode' => $postcode,
            'telephone' => $telephone
        ],
        'items'=> [ 
            //array of product which order you want to create
            ['product_id'=>'1','qty'=>1],
            ['product_id'=>'2','qty'=>2]
         ]
        ]
        ;
0

2 Answers 2

2

Something like this, I suppose:

$orderData = [
    'email'        => $customerEmail, //buyer email id
    'shipping_address' => [
        'firstname'    => $firstname, //address Details
        'lastname'     => $lastname,
        'street' => $address,
        'city' => $city,
        'country_id' => $countryid,
        'region' => $region,
        'regionId' => $regionid,
        'postcode' => $postcode,
        'telephone' => $telephone
    ],
    'items'=> []
];
foreach ($ItemCollection as $item) {
    // append data to 'items' subarray
    $orderData['items'][] = [
        'product_id' => $item['order_item_id'],
        'qty' => $item['qty'],
    ];
} 
Sign up to request clarification or add additional context in comments.

Comments

2

First structure your base $orderData array and initialize "items" as an empty array within $orderData. After that, you can build your items and push them to $orderData['items']:

<?php
$orderData = [
    'email' => $customerEmail, //buyer email id
    'shipping_address' => [
        'firstname' => $firstname, //address Details
        'lastname' => $lastname,
        'street' => $address,
        'city' => $city,
        'country_id' => $countryid,
        'region' => $region,
        'regionId' => $regionid,
        'postcode' => $postcode,
        'telephone' => $telephone
    ],
    'items' => [] // Initialize as empty array
];

foreach ($ItemCollection as $item) {
    // Build order item data
    $orderItem = [
        'product_id' => $item['order_item_id'],
        'qty' => $item['qty']
    ];

    $orderData['items'][] = $orderItem; // Push into items array of order data array
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.