0

I have two separate arrays.

Array 1:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Product 1
            [quantity] => 2
            [unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 
                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Product 2
            [quantity] => 4
            [unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 
                )

        )

    [2] => Array
        (
            [id] => 3
            [name] => Product 3
            [quantity] => 6
            [unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 
                )

        )

)

and Array 2:

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
)

I don't know how to make [value] in each array from Array 1 receive consecutive values from Array 2. What I need is this:

Array 1:

[unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 3
                )
[unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 4
                )
[unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 5
                )

This is how Array 1 is created. It's contained in $products_details[]:

<?php
foreach ($basket as $product) {
    $product_detail['id'] = $product['product_id'];
    $product_detail['name'] = $product['product_name'];
    $product_detail['quantity'] = $product['product_quantity'];
    $product_detail['unit_amount']['currency_code'] = $currency_code;
    $product_detail['unit_amount']['value'] = '';

    $products_details[] = $product_detail;
?>

This is how Array 2 is created:

<?php
foreach ($basket_prices as $price) {
    $product_detail_more[] = $price;
}
$products_details_more[] = $product_detail_more;
?>

The combination of both arrays should be contained in $items[].

Here is an example of what I have tried:

<?php
foreach ($products_details as $arr) {
    $arr['unit_amount']['value'] = $price;
    $items[] = $arr;
}
?>

but it adds only the last value from Array 2:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Product 1
            [quantity] => 2
            [unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 5 // The last value from Array 2 received.
                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Product 2
            [quantity] => 4
            [unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 5 // The last value from Array 2 received.
                )

        )

    [2] => Array
        (
            [id] => 3
            [name] => Product 3
            [quantity] => 6
            [unit_amount] => Array
                (
                    [currency_code] => GBP
                    [value] => 5 // The last value from Array 2 received.
                )

        )

)

Any help would be much appreciated.

1 Answer 1

1

Assuming all the arrays are all available. And I am assuming that Array 2: is the $basket_prices array

foreach ($basket as $i => $product) {
// add an index     ^^
    $product_detail['id'] = $product['product_id'];
    $product_detail['name'] = $product['product_name'];
    $product_detail['quantity'] = $product['product_quantity'];
    $product_detail['unit_amount']['currency_code'] = $currency_code;
    $product_detail['unit_amount']['value'] = $basket_prices[$i];
// use the index to address the other array   ^^^^^^^^^^^^^^^^^^^

    $products_details[] = $product_detail;
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, Array 2 is $basket_prices and all the arrays are available. I've tried your code. It gives me two errors: Undefined offset: 3, Undefined offset: 4 which seems to be related with $basket_prices[$i].
Well that means your basket array is bigger than your basket_prices array! What would you do about that in real life
Actually, the array basket contains those values too. (I was passing the same values via two different routes for certain reason.) So, instead of $basket_prices[$i] I put $product['product_price'][$i] and it works now. I don't need the other array. I think I was making the problem more complex than it really was. Many thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.