Array (4) {[0] = = Array (7) {["goodsid"] + = string (2) "" ["goodsno"] = + string (13) "9311770592581" [" Goodsname "] = = string (PNS)" DCS Super Aqua Foam Face Wash/Cleanser "[" price "] = + string (5)" 40.00 "[" Currency "] + string (2)" 15 " ["Quantity"] = + int (1) ["goods_tax_price"] = + string (1) "0"} [1] = = Array (7) {["goodsid"] = Strin G (2) "+" ["Goodsno"] + string (+) "9327693000805" ["Goodsname"] + string (+) "DCS Super Nutrition Essence" ["price"] = String (5) "10.00" ["Currency"] + string (2) "["] ["quantity"] + = string (1) "1" ["goods_tax_price"] = s Tring (1) "0"} [2] = = Array (7) {["Goodsid"] + string (2) "" ["goodsno"] = string (13) "9327693000744" ["goodsname"] = + string (+) "DCS Super Nutrient Water" ["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) "[" GoodSNo "] = string (40.00)" 9311770592505 "[" Goodsname "] and" DCs concentrated whitening Vitality Mask "[" Price "] and string (5)" "[" Currency "] + + string (2)" ["] [" quantity "] = + string (1)" 1 "[" goods_tax_price "] = + string (1)" 0 "}[4 ] = = Array (7) {["goodsid"] + = string (2) "" ["goodsno"] = = String ("9311770592505" ["Goodsname"] =& Gt String (+) "DCs Concentrated whitening Vitality Mask" ["price"] and + string (5) "40.00" ["currency"] = + string (2) "["] ["quantity"] = S Tring (1) "2" ["goods_tax_price"] = + string (1) "0"}}
The GOODSID repeats the product information in the array to synthesize one, the number is the sum of the quantity (same commodity sum)
The result should be:
Array (4) {[0] = = Array (7) {["goodsid"] + = string (2) "" ["goodsno"] = + string (13) "9311770592581" [" Goodsname "] = = string (PNS)" DCS Super Aqua Foam Face Wash/Cleanser "[" price "] = + string (5)" 40.00 "[" Currency "] + string (2)" 15 " ["Quantity"] = + int (1) ["goods_tax_price"] = + string (1) "0"} [1] = = Array (7) {["goodsid"] = Strin G (2) "+" ["Goodsno"] + string (+) "9327693000805" ["Goodsname"] + string (+) "DCS Super Nutrition Essence" ["price"] = String (5) "10.00" ["Currency"] + string (2) "["] ["quantity"] + = string (1) "1" ["goods_tax_price"] = s Tring (1) "0"} [2] = = Array (7) {["Goodsid"] + string (2) "" ["goodsno"] = string (13) "9327693000744" ["goodsname"] = + string (+) "DCS Super Nutrient Water" ["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) "[" GoodSNo "] = string (40.00)" 9311770592505 "[" Goodsname "] and" DCs concentrated whitening Vitality Mask "[" Price "] and string (5)" "[" Currency "] + + string (2)" ["] [" quantity "] = + string (1)" 3 "[" goods_tax_price "] = = string (1)" 0 "}}
Reply content:
Array (4) {[0] = = Array (7) {["goodsid"] + = string (2) "" ["goodsno"] = + string (13) "9311770592581" [" Goodsname "] = = string (PNS)" DCS Super Aqua Foam Face Wash/Cleanser "[" price "] = + string (5)" 40.00 "[" Currency "] + string (2)" 15 " ["Quantity"] = + int (1) ["goods_tax_price"] = + string (1) "0"} [1] = = Array (7) {["goodsid"] = Strin G (2) "+" ["Goodsno"] + string (+) "9327693000805" ["Goodsname"] + string (+) "DCS Super Nutrition Essence" ["price"] = String (5) "10.00" ["Currency"] + string (2) "["] ["quantity"] + = string (1) "1" ["goods_tax_price"] = s Tring (1) "0"} [2] = = Array (7) {["Goodsid"] + string (2) "" ["goodsno"] = string (13) "9327693000744" ["goodsname"] = + string (+) "DCS Super Nutrient Water" ["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) "[" GoodSNo "] = string (40.00)" 9311770592505 "[" Goodsname "] and" DCs concentrated whitening Vitality Mask "[" Price "] and string (5)" "[" Currency "] + + string (2)" ["] [" quantity "] = + string (1)" 1 "[" goods_tax_price "] = + string (1)" 0 "}[4 ] = = Array (7) {["goodsid"] + = string (2) "" ["goodsno"] = = String ("9311770592505" ["Goodsname"] =& Gt String (+) "DCs Concentrated whitening Vitality Mask" ["price"] and + string (5) "40.00" ["currency"] = + string (2) "["] ["quantity"] = S Tring (1) "2" ["goods_tax_price"] = + string (1) "0"}}
The GOODSID repeats the product information in the array to synthesize one, the number is the sum of the quantity (same commodity sum)
The result should be:
Array (4) {[0] = = Array (7) {["goodsid"] + = string (2) "" ["goodsno"] = + string (13) "9311770592581" [" Goodsname "] = = string (PNS)" DCS Super Aqua Foam Face Wash/Cleanser "[" price "] = + string (5)" 40.00 "[" Currency "] + string (2)" 15 " ["Quantity"] = + int (1) ["goods_tax_price"] = + string (1) "0"} [1] = = Array (7) {["goodsid"] = Strin G (2) "+" ["Goodsno"] + string (+) "9327693000805" ["Goodsname"] + string (+) "DCS Super Nutrition Essence" ["price"] = String (5) "10.00" ["Currency"] + string (2) "["] ["quantity"] + = string (1) "1" ["goods_tax_price"] = s Tring (1) "0"} [2] = = Array (7) {["Goodsid"] + string (2) "" ["goodsno"] = string (13) "9327693000744" ["goodsname"] = + string (+) "DCS Super Nutrient Water" ["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) "[" GoodSNo "] = string (40.00)" 9311770592505 "[" Goodsname "] and" DCs concentrated whitening Vitality Mask "[" Price "] and string (5)" "[" Currency "] + + string (2)" ["] [" quantity "] = + string (1)" 3 "[" goods_tax_price "] = = string (1)" 0 "}}
Don't say much nonsense, just go to the code:
$arr = array( '0' => array( 'id' => 1, 'count' =>1, ), '1' => array( 'id' => 2, 'count' =>1, ), '2' => array( 'id' => 4, 'count' =>1, ), '3' => array( 'id' => 2, 'count' =>1, ), );$new = $news = $newss = array();foreach ($arr as $key => $value) { if(!in_array($value['id'], $new)) { $new[] = $value['id']; //$new保存不重复的id值 $news[$key] = $value; //$news保存不重复id的数组值 $newss[$value['id']] = $key; //$newss保存不重复的id的键值 }else { $k = $newss[$value['id']]; //取出重复的id保存的第一个键值 $count = (int)$news[$k]['count']; //取出第一个相同id保存的count值 $news[$k]['count'] = $count+1; //赋值到新的数组 }}var_dump($news);
Say one idea, with a dictionary.
Because Goodsid is the only one, so K is Goodsid
Iterate through the array, if there is goodsid in the dictionary, then add the corresponding value, no then put the corresponding data in the dictionary
Two-dimensional array to go heavy and see if you want a result: Http://zhidao.baidu.com/link?...
This data is from the database inside out, if it is so to be heavy can rely on SQL query deduplication, if it is later merge can find the PHP version of the iterator class, the operation can be simple, but it is always unavoidable to iterate through the array.
Is this a de-weight problem for a two-dimensional array? If so, I look for the previous code, once wrote a function like this.