Skip to content
Advertisement

how to create dynamic array?

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

Advertisement

Answer

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'],
    ];
} 
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement