how to join two multidimensional arrays in php? I have two multidimensional arrays A and B. I need to join A and B to form a new array C as follows
如何在php中加入两个多维数组?我有两个多维数组A和B.我需要连接A和B以形成一个新的数组C,如下所示
$A = array(
array("a1"=>1,"b1"=>2,"c1"=>"A"),
array("a1"=>1,"b1"=>16,"c1"=>"Z"),
array("a1"=>3,"b1"=>8,"c1"=>"A"));
$B = array(
array("a2"=>1,"b2"=>2,"b2"=>"A"),
array("a2"=>1,"b2"=>16,"b2"=>"G"),
array("a2"=>3,"b2"=>8,"b2"=>"A"));
//join A and B to form C
//加入A和B以形成C.
$C=array(
array("a1"=>1,"b1"=>2,"c1"=>"A"),
array("a1"=>1,"b1"=>16,"c1"=>"Z"),
array("a1"=>3,"b1"=>8,"c1"=>"A"),
array("a2"=>1,"b2"=>2,"b2"=>"A"),
array("a2"=>1,"b2"=>16,"b2"=>"G"),
array("a2"=>3,"b2"=>8,"b2"=>"A"));
5 个解决方案
#1
11
Use the array_merge
function, like this:
使用array_merge函数,如下所示:
$C = array_merge($A, $B);
print_r($C);
When I run the above script it'll output:
当我运行上面的脚本时它会输出:
Array (
[0] => Array (
[a1] => 1
[b1] => 2
[c1] => A
)
[1] => Array (
[a1] => 1
[b1] => 16
[c1] => Z )
[2] => Array (
[a1] => 3
[b1] => 8
[c1] => A
)
[3] => Array (
[a2] => 1
[b2] => A
)
[4] => Array (
[a2] => 1
[b2] => G
)
[5] => Array (
[a2] => 3
[b2] => A
)
)
Take a quick read here: http://php.net/manual/function.array-merge.php
快速阅读一下:http://php.net/manual/function.array-merge.php
#3
3
Did you try some PHP array functions? I think some of these work: array_merge_recursive()
, array_merge()
.
你尝试过一些PHP数组函数吗?我认为其中一些工作:array_merge_recursive(),array_merge()。
$array1 = array("farbe" => "rot", 2, 4);
$array2 = array("a", "b", "farbe" => "grün", "form" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
#4
1
you can also do this as
你也可以这样做
foreach($B as $key => $value){
$C[$key] = array_merge($A[$key], $B[$key]);
}
#5
0
Hi I have faced same situation where i had to show duplicate events in calendar
嗨,我遇到了同样的情况,我不得不在日历中显示重复的事件
So i used this solution:-
所以我用这个解决方案: -
public function array_interlace() {
$args = func_get_args();
$total = count($args);
if($total < 2) {
return FALSE;
}
$i = 0;
$j = 0;
$arr = array();
foreach($args as $arg) {
foreach($arg as $v) {
$arr[$j] = $v;
$j += $total;
}
$i++;
$j = $i;
}
ksort($arr);
return array_values($arr);
}
Required output of array
数组所需的输出
$a = array('a', 'b', 'c', 'd');
$b = array('a','e', 'f', 'g');
$c = array('h', 'i', 'j');
$d = array('k', 'l', 'm', 'n', 'o');
print_r(array_interlace($a, $b, $c, $d));
Array
(
[0] => a
[1] => a
[2] => h
[3] => k
[4] => b
[5] => e
[6] => i
[7] => l
[8] => c
[9] => f
[10] => j
[11] => m
[12] => d
[13] => g
[14] => n
[15] => o
)
I found this solution suitable..
我发现这个解决方案适合..
#1
11
Use the array_merge
function, like this:
使用array_merge函数,如下所示:
$C = array_merge($A, $B);
print_r($C);
When I run the above script it'll output:
当我运行上面的脚本时它会输出:
Array (
[0] => Array (
[a1] => 1
[b1] => 2
[c1] => A
)
[1] => Array (
[a1] => 1
[b1] => 16
[c1] => Z )
[2] => Array (
[a1] => 3
[b1] => 8
[c1] => A
)
[3] => Array (
[a2] => 1
[b2] => A
)
[4] => Array (
[a2] => 1
[b2] => G
)
[5] => Array (
[a2] => 3
[b2] => A
)
)
Take a quick read here: http://php.net/manual/function.array-merge.php
快速阅读一下:http://php.net/manual/function.array-merge.php
#2
#3
3
Did you try some PHP array functions? I think some of these work: array_merge_recursive()
, array_merge()
.
你尝试过一些PHP数组函数吗?我认为其中一些工作:array_merge_recursive(),array_merge()。
$array1 = array("farbe" => "rot", 2, 4);
$array2 = array("a", "b", "farbe" => "grün", "form" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
#4
1
you can also do this as
你也可以这样做
foreach($B as $key => $value){
$C[$key] = array_merge($A[$key], $B[$key]);
}
#5
0
Hi I have faced same situation where i had to show duplicate events in calendar
嗨,我遇到了同样的情况,我不得不在日历中显示重复的事件
So i used this solution:-
所以我用这个解决方案: -
public function array_interlace() {
$args = func_get_args();
$total = count($args);
if($total < 2) {
return FALSE;
}
$i = 0;
$j = 0;
$arr = array();
foreach($args as $arg) {
foreach($arg as $v) {
$arr[$j] = $v;
$j += $total;
}
$i++;
$j = $i;
}
ksort($arr);
return array_values($arr);
}
Required output of array
数组所需的输出
$a = array('a', 'b', 'c', 'd');
$b = array('a','e', 'f', 'g');
$c = array('h', 'i', 'j');
$d = array('k', 'l', 'm', 'n', 'o');
print_r(array_interlace($a, $b, $c, $d));
Array
(
[0] => a
[1] => a
[2] => h
[3] => k
[4] => b
[5] => e
[6] => i
[7] => l
[8] => c
[9] => f
[10] => j
[11] => m
[12] => d
[13] => g
[14] => n
[15] => o
)
I found this solution suitable..
我发现这个解决方案适合..