Home >Backend Development >PHP Tutorial >Two ways to merge two two-dimensional arrays into one two-dimensional array in PHP_PHP Tutorial
Title: There are the following 2 two-dimensional arrays
1
$a=Array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
2
$b=Array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
The contents of the two arrays are as above. How to merge $a and $b into a new two-dimensional array? The content of the new array is required to be as follows:
1
Array(0 => Array(id => 66, class_name => www.iiwnet.com, class_count => 8), 1 => Array(id => 67, class_name => linux, class_count => 2));
Someone must have encountered this problem. Someone asked it in the group today and I gave two solutions
The following code is originally provided by PHP Tutorial Network
01
Method 1:
02
03
Function arrpreg(){
04
$a=Array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
05
$b=Array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
06
$arr = array();
07
foreach ($a as $k => $r) {
08
foreach($r as $k1 => $r1){
09
$arr[$k][$k1] = $r1;
10
}
11
12
foreach ($b as $k => $r) {
13
foreach($r as $k1 => $r1){
14
$arr[$k][$k1] = $r1;
15
}
16
}
17
return $arr;
18
}
19
$ar= arrpreg();
20
echo '
';<br> 21<br> Print_r($ar);<br> 22<br> echo '';
';<br> 34<br> Print_r($arr);<br> 35<br> echo '';