Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65831768/adding-an-associative-array-to-another-associative-array-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...