dongqiu3254 2016-04-19 15:28
浏览 88

PHP合并数组数组和增量值?

I have the following array structure:

[0] => Array
    (
        [name] => Spinat boller
        [amount] => 3
        [category] => Bløddej og Boller
        [unit_amount] => 15
    )

[1] => Array
    (
        [name] => Bedste boller
        [amount] => 15
        [category] => Bløddej og Boller
        [unit_amount] => 15
    )

[2] => Array
    (
        [name] => Grov boller
        [amount] => 15
        [category] => Bløddej og Boller
        [unit_amount] => 15
    )

I have multiple of these arrays, and i would like to merge them on the AMOUNT KEY ONLY. That means if i have another array containing this:

[0] => Array
    (
        [name] => Spinat boller
        [amount] => 2
        [category] => Bløddej og Boller
        [unit_amount] => 15
    )

And i merge it, it would show this:

[0] => Array
    (
        [name] => Spinat boller
        [amount] => 5
        [category] => Bløddej og Boller
        [unit_amount] => 15
    )

Obviously i could traverse this manually, but i was hoping i could utilize the PHP Merge function somehow, but not sure how to go about it.

Thanks everyone!

* Edit 1 * If i just do the array_merge function, it simply appends it all together, so i go from:

[0] => Array
(
    [name] => Spinat boller
    [amount] => 3
    [category] => Bløddej og Boller
    [unit_amount] => 15
)

to:

[0] => Array
(
    [name] => Spinat boller
    [amount] => 3
    [category] => Bløddej og Boller
    [unit_amount] => 15
)
[0] => Array
(
    [name] => Spinat boller
    [amount] => 2
    [category] => Bløddej og Boller
    [unit_amount] => 15
)

but i want:

[0] => Array
(
    [name] => Spinat boller
    [amount] => 3
    [category] => Bløddej og Boller
    [unit_amount] => 15
)

[0] => Array ( [name] => Spinat boller [amount] => 5 [category] => Bløddej og Boller [unit_amount] => 15 )

Notice how the amount is now 5 (the other arrays had 2 and 3). Everything else is the same and matches, ONLY the amount gets added and incremented.

  • 写回答

1条回答 默认 最新

  • dousenjue3214 2016-04-20 18:08
    关注

    So I gave it some thought, to see if it's possible to do it in a single expression.

    You have to combine quite a few functions, but the behaviour is the same as with manual iteration. The result of this functional approach looks like this:

    $result = array_values(
        array_merge(
            ...array_reverse(
                array_map(function ($arr) {
                    return array_combine(
                        array_column($arr, 'name'),
                        array_values($arr)
                    );
                }, [$arr1, $arr2, $arr3])
            )
        )
    );
    

    Though it can be done, I sticking with the iteration approach would be best for maintainability reasons:

    $tmp = [];
    
    foreach ($arrays as $arr) {
        foreach ($arr as $elem) {
            if (!isset($tmp[$elem['name']])) {
                $tmp[$elem['name']] = $elem;
            }
        }
    }
    
    $result = array_values($tmp);
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向