简体   繁体   中英

Adding an associative array to another associative array in PHP

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.

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;

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