php數組重複資料的處理

WBOY
發布: 2016-10-17 09:30:14
原創
1094 人瀏覽過

<code>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"
  }
}</code>
登入後複製
登入後複製

把陣列裡的goodsId重複的商品資訊合成一個,個數為quantity的總和(同樣商品總和)

結果應為:

<code>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"
  }

}</code>
登入後複製
登入後複製

回覆內容:

<code>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"
  }
}</code>
登入後複製
登入後複製

把陣列裡的goodsId重複的商品資訊合成一個,個數為quantity的總和(同樣商品總和)

結果應為:

<code>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"
  }

}</code>
登入後複製
登入後複製

廢話不多說,直接上程式碼吧:

<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);</code>
登入後複製

php數組重複資料的處理

說一個思路吧,用一個字典
因為goodsId是唯一的, 所以k是goodsId
遍歷數組,如果字典裡有goodsId則加上對應的數值,沒有則把對應的資料放到字典裡

二維數組去重,看看這個是否你想要個結果:http://zhidao.baidu.com/link?...

這些資料是從資料庫裡面查出來的麼,如果是那麼要去重可以依賴sql查詢去重,如果是後來merge的可以找找PHP版的迭代器類,操作可以簡單一點,但這樣始終免不了遍歷數組的。

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

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板