string(2) "15" ["goodsNo"] => string(13) "9311770592581" ["goodsNa">
当前位置:Gxlcms > PHP教程 > php数组重复数据的处理

php数组重复数据的处理

时间:2021-07-01 10:21:17 帮助过:31人阅读

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版的迭代器类,操作可以简单一点,但这样始终免不了遍历数组的。

这是一个二维数组的去重问题吗?如果是的话,我找找以前的代码,曾经写过这样一个函数。

人气教程排行