php数组重复数据的处理 财富值87

2016-10-12 17:44发布

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) "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"   }  }
2条回答

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

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

一周热门 更多>