php合并相同数据 php两种复合数据类型
合并PHP数据中的相同项
?php
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的黄岩网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
$result=array();
foreach($data as $val){
$tmp=isset( $result[$val["offer_id"]]["country"])? $result[$val["offer_id"]]["country"]:"";
$result[$val["offer_id"]]["country"]=$tmp.$val["country"];
$tmp=isset( $result[$val["offer_id"]]["platfrom"])? $result[$val["offer_id"]]["platfrom"]:"";
$result[$val["offer_id"]]["platfrom"]=$tmp.$val["platfrom"];
$tmp=isset( $result[$val["offer_id"]]["click"])? $result[$val["offer_id"]]["click"]:0;
$result[$val["offer_id"]]["click"]=$tmp+$val["click"];
}
print_r($result);
thinkphp中两条数据有相同的内容,如何将他们合并成一条数据
1. 在用mysql查询的时候,用group by (某一个字段) 将其查询结果合并;
或者;
2. 查询条件用limit 1 限制查询结果条数,反正内容一样,你就要一条即可;
php 合并多维数组中相同的值
$arr = array(
array(
array(
"stocknotifyId" = 1,
"sku" = '13809_BL_37',
"email" = 'huang@aaa.com'
),
array(
"stocknotifyId" = 3,
"sku" = '13809_BL_37',
"email" = 'hrheve@dfwef.com'
),
array(
"stocknotifyId" = 5,
"sku" = '13809_BL_37',
"email" = 'dfsfs@dfsf.com'
)
),
array(
array(
"stocknotifyId" = 2,
"sku" = '13809_B_39',
"email" = 'huang@aaa.com'
)
),
array(
array(
"stocknotifyId" = 4,
"sku" = '13809_BL_39',
"email" ='hrheve@dfwef.com'
)
)
);
$re = array();
foreach($arr as $a) {
foreach ($a as $v) {
$tmp_v = $v;
unset($tmp_v['email']);
if(isset($re[$v['email']])) {
$re[$v['email']][] = $tmp_v;
}else{
$re[$v['email']] = array($tmp_v);
}
}
}
print_r($re);
!-- output --
Array
(
[huang@aaa.com] = Array
(
[0] = Array
(
[stocknotifyId] = 1
[sku] = 13809_BL_37
)
[1] = Array
(
[stocknotifyId] = 2
[sku] = 13809_B_39
)
)
[hrheve@dfwef.com] = Array
(
[0] = Array
(
[stocknotifyId] = 3
[sku] = 13809_BL_37
)
[1] = Array
(
[stocknotifyId] = 4
[sku] = 13809_BL_39
)
)
[dfsfs@dfsf.com] = Array
(
[0] = Array
(
[stocknotifyId] = 5
[sku] = 13809_BL_37
)
)
)
PHP如何合并数组当中有相同数值的数组
array_unique函数提供了过滤重复的值的功能,但是该函数只能处理一维数组,如果遇到多维数组我们可以现将其转化为一维数组。示例代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* $data 是你要处理数据
* $result 是输出的结果
*/
//将其转化为一维数组
foreach ($data as $row) {
$row = serialize($row);
}
unset($row);
$data = array_unique($data);
//还原多维数组
foreach ($data as $row) {
$row = unserialize($row);
}
unset($row);
不好意思 楼主要求,我看错了,追加下面的代码
1
2
3
4
5
6
7
8
9
10
11
12
$result=array();
$tmp_arr=array();
foreach ($data as $row) {
$tmp_arr[$row['confuguarable_sku']][] = $row['size'];
}
//将尺寸用逗号拼接
foreach ($tmp_arr as $sku = $size) {
$result[] = array('confuguarable_sku'=$sku, 'size'=implode(",", $size));
}
echo "pre";
//输出结果
print_r($result);
新闻标题:php合并相同数据 php两种复合数据类型
URL网址:http://pwwzsj.com/article/hhdhdg.html