PHP合并多维数组并加值

问题描述 投票:0回答:0

我有这个数组:

$products2 = [
    [
        "idProduct" => 11001000,
        "price" => "320.00",
        "currency" => "USD",
        "Description" => "shoes",
    ],
    [
        "idProduct" => 11001001,
        "price" => "320.00",
        "currency" => "USD",
        "Description" => "shoes",
    ],
    [
        "idProduct" => 11001002,
        "price" => "220.00",
        "currency" => "USD",
        "Description" => "shirt",
    ],
    [
        "idProduct" => 11001003,
        "price" => "220.00",
        "currency" => "USD",
        "Description" => "shirt",
    ],
];

使用 print_r 输出:

Array
(
    [0] => Array
        (
            [idProduct] => 11001000
            [price] => 320.00
            [currency] => USD
            [Description] => shoes
        )

    [1] => Array
        (
            [idProduct] => 11001001
            [price] => 320.00
            [currency] => USD
            [Description] => shoes
        )

    [2] => Array
        (
            [idProduct] => 11001002
            [price] => 220.00
            [currency] => USD
            [Description] => shirt
        )

    [3] => Array
        (
            [idProduct] => 11001003
            [price] => 220.00
            [currency] => USD
            [Description] => shirt
        )

)

我的问题是如何使用前一个数组的值创建以下数组,添加价格字段并成对存储 idProduct:

Array
(
    [0] => Array
        (
            [idProduct] => Array
                (
                    [0] => 11001000
                    [1] => 11001001
                )

            [price] => 640.00
            [currency] => USD
            [Description] => shoes
        )

    [1] => Array
        (
            [idProduct] => Array
                (
                    [0] => 11001002
                    [1] => 11001003
                )

            [price] => 4400.00
            [currency] => USD
            [Description] => shirt
        )

)

非常感谢您能给我的任何帮助。

我尝试使用 foreach 遍历产品数组并创建新的产品对数组,但没有成功。

php arrays multidimensional-array dynamic-arrays
© www.soinside.com 2019 - 2024. All rights reserved.