本文目录一览:
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中,合并一个数组里相同的值。
直接用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将数组键值相同的数组合并数组为一个数组
把'huaka'改成了'huakai'似乎是一个人
?php
$a=array(
array("username"="piaoyudesi","email"="piaoyudesi@163.com","address"="shenzhen","item_id"="10058","item_title"="BPS8"),
array("username"="huakai","email"="huakai@163.com","address"="zhongshan","item_id"="10087","item_title"="BPS9"),
array("username"="huakai","email"="huakai@163.com","address"="zhongshan","item_id"="10047","item_title"="BPS10"),
array("username"="piaoyudesi","email"="piaoyudesi@163.com","address"="shenzhen","item_id"="10042","item_title"="BPS10"),
array("username"="jimowenxin","email"="jimowenxin@163.com","address"="zhuhai","item_id"="10031","item_title"="BPS18")
);
$u=array();
foreach($a as $k=$e){
$name=$e['username'];
if(!isset($u[$name])){
$u[$name]=$e;
unset($u[$name]['item_id'],$u[$name]['item_title']);
}
$u[$name]['goods'][]=array('item_id'=$e['item_id'],'item_title'=$e['item_title']);
}
$a=array_values($u); unset($u);
print_r($a);
=========
Array
(
[0] = Array
(
[username] = piaoyudesi
[email] = piaoyudesi@163.com
[address] = shenzhen
[goods] = Array
(
[0] = Array
(
[item_id] = 10058
[item_title] = BPS8
)
[1] = Array
(
[item_id] = 10042
[item_title] = BPS10
)
)
)
[1] = Array
(
[username] = huakai
[email] = huakai@163.com
[address] = zhongshan
[goods] = Array
(
[0] = Array
(
[item_id] = 10087
[item_title] = BPS9
)
[1] = Array
(
[item_id] = 10047
[item_title] = BPS10
)
)
)
[2] = Array
(
[username] = jimowenxin
[email] = jimowenxin@163.com
[address] = zhuhai
[goods] = Array
(
[0] = Array
(
[item_id] = 10031
[item_title] = BPS18
)
)
)
)
php 合并相同元素数组
先合并
array array_merge ( array $array1 [, array $... ] );
再去重
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
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
)
)
)