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" }}
回複內容:
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" }}
廢話不多說,直接上代碼吧:
$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);
說一個思路吧,用一個字典
因為goodsId是唯一的, 所以k是goodsId
遍曆數組,如果字典裡有goodsId則加上對應的數值,沒有則把對應的資料放到字典裡
二維數組去重,看看這個是否你想要個結果:http://zhidao.baidu.com/link?...
這些資料是從資料庫裡面查出來的麼,如果是那麼要去重可以依賴sql查詢去重,如果是後來merge的可以找找PHP版的迭代器類,操作可以簡單一點,但這樣始終免不了遍曆數組的。
這是一個二維數組的去重問題嗎?如果是的話,我找找以前的代碼,曾經寫過這樣一個函數。