PHP merges two-dimensional arrays into one two-dimensional array. 2 solutions _ PHP Tutorial

Source: Internet
Author: User
PHP combines two-dimensional arrays and one two-dimensional array. Question: There are the following two 2D arrays: 1 $ aArray (0 Array (id66, class_namewww.iiwnet.com), 1 Array (id67, class_namelinux), 2 $ bArray (0 Array (class_count8 ), 1 Array question: there are the following two 2D 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 '
';

Rows 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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.