问答

php数组重复数据的处理?

泡妞宝典 浏览量:0 | 2023-06-20 14:24
+关注

array(4) { [0] => array(7) { ["goodsId"] => string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsName"] =&<爱尬聊_百科>gt; string(37) "DCS超级水润泡沫洗面/洁面乳" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => int(1) ["goods_tax_price"] => string(1) "0" } [1] => array(7) { ["goodsId"] => string(2) "16" ["goodsNo"] => string(13) "9327693000805" ["goodsName"] => string(21) "DCS超级营养精华" ["price"] => string(5) "10.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [2] => array(7) { ["goodsId"] => string(2) "17" ["goodsNo"] => string(13) "9327693000744" ["goodsName"] => string(18) "DCS超级营养水" ["price"] => string(5) "30.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [3] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [4] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "2" ["goods_tax_price"] => string(1) "0" } }

把数组里的goodsId重复的商品信息合成一个,个数为quantity的总和(同样商品总和)

结果应为:

array(4) { [0] => array(7) { ["goodsId"] => string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsName"] => string(37) "DCS超级水润泡沫洗面/洁面乳" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => int(1) ["goods_tax_price"] => string(1) "0" } [1] => array(7) { ["goodsId"] => string(2) "16" ["goodsNo"] => string(13) "9327693000805" ["goodsName"] => string(21) "DCS超级营养精华" ["price"] => string(5) "10.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [2] => array(7) { ["goodsId"] => string(2) "17" ["goodsNo"] => string(13) "9327693000744" ["goodsName"] => string(18) "DCS超级营养水" ["price"] => string(5) "30.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "1" ["goods_tax_price"] => string(1) "0" } [3] => array(7) { ["goodsId"] => string(2) "18" ["goodsNo"] => string(13) "9311770592505" ["goodsName"] => string(27) "DCS集中美白活力面膜" ["price"] => string(5) "40.00" ["currency"] => string(2) "15" ["quantity"] => string(1) "3" ["goods_tax_price"] => string(1) "0" }}


xymm敏敏 12小时前

二维数组去重,看看这个是否你想要个结果:http://zhidao.baidu.com/link?...


asrwetwtw 12小时前

说一个思路吧,用一个字典因为goodsId是唯一的, 所以k是goodsId遍历数组,如果字典里有goodsId则加上对应的数值,没有则把对应的数据放到字典里


文章剩余50%

精彩推荐

RECOMMEND

热门评论

TOP COMMENTS

    暂无评论...
验证码 换一张
立即登录评论
分享到
取消