微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

合并重复的键值并在动态多维数组中求和

如何解决合并重复的键值并在动态多维数组中求和

目前我有一个数据保存在 MysqL 的 json 列中。我想显示数据,但我需要合并重复值并对特定键的值求和。数组的键是动态的。这是示例响应。

[
 [
    {
        "FeeType": "Commission","FeeAmount": {
            "CurrencyCode": "EUR","CurrencyAmount": 1.75
        }
    },{
        "FeeType": "refundCommission","CurrencyAmount": -0.35
        }
    }
 ],[
    {
        "ChargeType": "Tax","ChargeAmount": {
            "CurrencyCode": "EUR","CurrencyAmount": 0
        }
    },{
        "ChargeType": "Tax",{
        "ChargeType": "Principal","CurrencyAmount": -24.95
        }
    },{
        "ChargeType": "ReturnShipping","CurrencyAmount": 0
        }
    }
],[
    {
        "FeeType": "Commission","CurrencyAmount": -0.35
        }
    }
],"CurrencyAmount": -24.95
        }
    }
],{
        "FeeType": "ShippingChargeback","CurrencyAmount": 3.35
        }
    }
],[
    {
        "ChargeType": "ShippingTax",{
        "ChargeType": "ShippingCharge","CurrencyAmount": -3.99
        }
    }
],[
    {
        "PromotionId": "DE Core Free Shipping 2018\/04\/24 20-00-00","PromotionType": "PromotionMetaDataDeFinitionValue","PromotionAmount": {
            "CurrencyCode": "EUR",{
        "PromotionId": "DE Core Free Shipping 2018\/04\/24 20-00-00","CurrencyAmount": 1.99
        }
    }
],{
        "ChargeType": "ShippingTax","CurrencyAmount": -1.99
        }
    },

........

我想要达到的是有这样的结果

   [
        ShippingCharge => totalofShippingCharges
        Tax => totalofalltax
        Principal => totalofall pricipla,Commission => totalofallcommissions
   ]

我试过是这样的

    $result = collect($myarray)
        ->groupBy('FeeType')
        ->map(function( $value) {
             return array_merge(...$value->toArray());
        })->values()->toArray();

   

但它只是创建原始数组的新副本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。