如何在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"));
//加入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"));
答案 0 :(得分:12)
使用array_merge
功能,如下所示:
$C = array_merge($A, $B);
print_r($C);
当我运行上面的脚本时,它会输出:
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
)
)
答案 1 :(得分:4)
$C = array_merge($A, $B);
应该做的伎俩(docs)。
答案 2 :(得分:3)
您是否尝试过一些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);
答案 3 :(得分:1)
你也可以这样做
foreach($B as $key => $value){
$C[$key] = array_merge($A[$key], $B[$key]);
}
答案 4 :(得分:0)
您好我面临同样的情况,我必须在日历中显示重复的事件
所以我使用了这个解决方案: -
<?php
$arr = Array(Array
(
'id' => 13,
'pets' => 8,
'num_of_times' => 3
),Array(
'id' => 15,
'pets' => 8,
'num_of_times' => 6
),Array (
'id' => 16,
'pets' => 10,
'num_of_times' => 2
),Array(
'id' => 17,
'pets' => 9,
'num_of_times' => 4
),Array(
'id' => 18,
'pets' => 10,
'num_of_times' => 3
),Array(
'id' => 19,
'pets' => 10,
'num_of_times' => 10
),Array(
'id' => 20,
'pets' => 0,
'num_of_times' => 11
),Array(
'id' => 21,
'pets' => 8,
'num_of_times' => 9
),Array(
'id' => 22,
'pets' => 9,
'num_of_times' => 0
),Array(
'id' => 23,
'pets' => 4,
'num_of_times' => 3
),Array(
'id' => 24,
'pets' => 0,
'num_of_times' => 1
),Array(
'id' => 40,
'pets' => 8,
'num_of_times' => 0
),Array(
'id' => 43,
'pets' => 2,
'num_of_times' => 2,
));
$checker = array(); //array that holds the id with the count of use
$remove =array(); // array to remove in array to random
for ($i = 1; $i <= 10; $i++) {
$newArray =array_diff(array_column($arr, 'id', 'id'),$remove); //the newarray to use in random with remove function for the id that has been used for 3x
$ids = array_rand($newArray, 5);
foreach($ids as $id){
if(!array_key_exists($id,$checker)){ // check if the id is existing in the checker key if yes
$checker[$id] = 1; // insert value 1 in the array as the count of use
}else{
if(!empty($checker[$id]) && $checker[$id] < 3){ //check if the array has a value lower than 3
$checker[$id]=$checker[$id]+1; // then add 1 value for the data as a increment of data usage
if($checker[$id] == 3){ //check if da is already used 3x
$remove[]=$id; //then add it to the variable remove
}
}
}
}
echo implode(',',$ids).'</br>';
}
echo '<pre>';
echo print_r($checker);
echo '</pre>';
数组的必需输出
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);
}
我发现这个解决方案很合适..