首页 / PHP / php数组重复数据的处理
php数组重复数据的处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php数组重复数据的处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6012字,纯文字阅读大概需要9分钟。
内容图文
![php数组重复数据的处理](/upload/InfoBanner/zyjiaocheng/151/e64d689d2d484e0c85e3994bdde1c133.jpg)
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版的迭代器类,操作可以简单一点,但这样始终免不了遍历数组的。
这是一个二维数组的去重问题吗?如果是的话,我找找以前的代码,曾经写过这样一个函数。
内容总结
以上是互联网集市为您收集整理的php数组重复数据的处理全部内容,希望文章能够帮你解决php数组重复数据的处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。