本文目录一览:
- 1、php两个相关的多维数组怎么合并成一个数组
- 2、php中如何合并几个二维数组成为一个二维数组
- 3、php多维数组合并成一个数组,该如何处理
- 4、PHP多维数组的值进行合并
- 5、php 合并多维数组中相同的值
- 6、php 把多维数组中,某值相同的项合并,重组数据
php两个相关的多维数组怎么合并成一个数组
如果是两个数组是相对应的,可以使用以下方法:
?php
//数组1(gc_class):
$gc_class = array(
0 =array('gc_id'=1,'gc_name'='分类1', 'gc_title' = '111111'),
1 =array('gc_id'=2,'gc_name'='分类2', 'gc_title' = '2222')
);
//数组2(acticity):
$acticity = array(
'0'=array('activity_id'=1, 'gc_class'=1, 'activity_name'='分类1下面的活动'),
'1'=array('activity_id'=2, 'gc_class'=2, 'activity_name'='分类2下面的活动')
);
$arrs = array();
foreach($gc_class as $key = $_gc_class){
$arrs[$key] = array_merge($_gc_class, $acticity[$key]);
}
print_r($arrs);
exit;
php中如何合并几个二维数组成为一个二维数组
PHP 中的 array_merge_recursive() 函数可以实现
将一个或多个数组的元素的合并起来,一个数组中的值附加在前一个数组的后面。并返回作为结果的数组。
当有重复的键名时,值不会被覆盖,而是将多个相同键名的值递归组成一个数组。
array_merge_recursive(array1,array2,array3...)
参数说明
array1 必需。输入的第一个数组。
array2 必需。输入的第二个数组。
array3 可选。可指定的多个输入数组。
例如:
?php
$a1=array("a"="Horse","b"="Dog");
$a2=array("c"="Cow","b"="Cat");
print_r(array_merge_recursive($a1,$a2));
?
输出:
Array (
[a] = Horse
[b] = Array ( [0] = Dog [1] = Cat )
[c] = Cow
)
注:array_merge() 函数也可以实现,与 array_merge_recursive()函数不同是,如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的)。如果数组是数字索引的,则键名会以连续方式重新索引。
php多维数组合并成一个数组,该如何处理
给数组加上变量,然后遍历一下
foreach ($Arr1 as $k = $r) {
$Arr4[] = array($Arr1[$k],$Arr2[$k],$Arr3[$k]);
}
echo 'pre';
print_r($Arr4);
PHP多维数组的值进行合并
这个没有现成的函数可以用 只能写代码做了
$arrResult = array();
foreach ($arr as $oneItem) {
$arr[$oneItem['uid']] = array('uid' = $oneItem['uid'], 'name' = (array_key_exists('name', $arr[$oneItem['uid']]) $arr[$oneItem['uid']]['name'] != '') ? $oneItem['uid']]['name'] . ',' . $oneItem['name'] : $oneItem['name']);
}
$arr = array();
foreach ($arrResult as $oneUid) {
$arr[] = $oneUid;
}
$arr是你原来的数组变量 最后得到$arr是结果
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
$laArray = array(
0 = array("id"=11,"name"="happy","age"="20","moeny"=100),
1 = array("id"=29,"name"="lucy","age"="20","moeny"=100),
2 = array("id"=34,"name"="happy","age"="20","moeny"=235),
3 = array("id"=42,"name"="happy","age"="15","moeny"=100),
4 = array("id"=51,"name"="happy","age"="20","moeny"=600),
5 = array("id"=61,"name"="lucy","age"="20","moeny"=350),
6 = array("id"=61,"name"="lucy","age"="23","moeny"=150),
);
$temArr = $laArray ;
$newArr = array();
foreach($laArray as $num = $arr ){
unset($temArr[$num]);
$id = $arr['id'];
foreach($temArr as $tNum =$tArr ){
$n_arr = array();
$tem_moeny = array();
if($arr['name']== $tArr['name'] $arr['age']== $tArr['age']){
$id .= '+'.$tArr['id'];
$n_arr['id'] = $id;
$n_arr['name'] = $arr['name'];
$n_arr['age'] = $arr['age'];
if(is_array($arr['moeny'])){
array_push($n_arr['moeny'], $tArr['moeny']);
}else{
$tem_moeny [] = $arr['moeny'];
$tem_moeny [] = $tArr['moeny'];
}
$tem_moeny[] = $tArr['moeny'];
$n_arr['moeny'] = $tem_moeny;
$newArr[] = $n_arr ;
}
}
if($id == $arr['id']){
$newArr[] = $arr ;
}
}
print_r($newArr);
?