php數組重複資料的處理

來源:互聯網
上載者:User
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版的迭代器類,操作可以簡單一點,但這樣始終免不了遍曆數組的。

這是一個二維數組的去重問題嗎?如果是的話,我找找以前的代碼,曾經寫過這樣一個函數。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.