php数组合并重复数据 php找出数组中重复的数字
PHP如何合并数组当中有相同数值的数组
array_unique函数提供了过滤重复的值的功能,但是该函数只能处理一维数组,如果遇到多维数组我们可以现将其转化为一维数组。示例代码如下
创新互联建站从2013年创立,先为大柴旦等服务建站,大柴旦等地企业,进行企业商务咨询服务。为大柴旦企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
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
// 删除数组中相同元素,只保留一个相同元素
function formatArray($array)
{
sort($array);
$tem = "";
$temarray = array();
$j = 0;
for($i=0;$icount($array);$i++)
{
if($array[$i]!=$tem)
{
$temarray[$j] = $array[$i];
$j++;
}
$tem = $array[$i];
}
return $temarray;
}
//测试 调用函数
$array = array('aa','bb','aa',3,4,5,5,5,5,'bc');
$arr = formatArray($array);
print_r($arr);
?
php中,合并一个数组里相同的值。
直接用array_unique()函数即可
$arr = ['0006','0006','0005'];
print_r($arr); //Array ( [0] = 0006 [1] = 0006 [2] = 0005 )
$arr = array_unique($arr);
print_r($arr); //Array ( [0] = 0006 [2] = 0005 )
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数组合并重复数据 php找出数组中重复的数字
URL分享:http://ybzwz.com/article/hipeeh.html