Home  >  Article  >  Backend Development  >  Processing of duplicate data in php array

Processing of duplicate data in php array

WBOY
WBOYOriginal
2016-10-17 09:30:141091browse

<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>

Combine the product information with repeated goodsId in the array into one, and the number is the sum of quantity (the sum of the same products)

The result should be:

<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>

Reply content:

<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>

Combine the product information with repeated goodsId in the array into one, and the number is the sum of quantity (the sum of the same products)

The result should be:

<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>

No more nonsense, let’s go straight to the 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>

Processing of duplicate data in php array

Let’s talk about an idea, use a dictionary
Because goodsId is unique, so k is goodsId
Traverse the array, if there is goodsId in the dictionary, add the corresponding value, if not, put the corresponding data in the dictionary

Two-dimensional array deduplication, see if you want the result: http://zhidao.baidu.com/link?...

Are these data retrieved from the database? If so, you can rely on sql query to remove duplicates. If it is merged later, you can find the PHP version of the iterator class. The operation can be simpler, but traversal is always inevitable. array.

Is this a two-dimensional array deduplication problem? If so, I looked for the previous code and once wrote such a function.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn