Question: There are the following two 2D arrays: 1 $ aArray (0 & gt; Array (id & gt; 66, class_name & gt; www.iiwnet.com), 1 & gt; array (id & gt; 67, class_name & gt; linux); 2 $ bArray (0 & gt; Array (class_count &
Question: There are the following 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 content of the two arrays is 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 question. There are two solutions that can be provided if someone in the group asks me today.
The following code is provided by PHP Tutorial.
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'
';
21
print_r($ar);
22
echo '
';
23
?>
24
25
Method 2:
26
27
$ A = array (0 => Array (id => 66, class_name => www.iiwnet.com), 1 => Array (id => 67, class_name => linux ));
28
$ B = array (0 => Array (class_count => 8), 1 => Array (class_count => 2 ));
29
$ Arr = array ();
30
Foreach ($ a as $ k => $ r ){
31
$ Arr [] = array_merge ($ r, $ B [$ k]);
32
}
33
Echo'
';
34
print_r($arr);
35
echo '
';