Similarities and differences between the two methods of PHP merging two Arrays

Source: Internet
Author: User

In particular, the + operator, which means that the array unit on the right (deduplication) is appended to the back of the array on the left. Copy codeThe Code is as follows: <? Php
Echo "\ r \ n first case \ r \ n ";
$ A = array (1, 2, 3, 4, 5, 6 );
$ B = array (7,8, 9 );
$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
Echo "\ r \ n case 2 \ r \ n ";
$ A = array ('A', 'B', 'C', 'D', 'E', 'F ');
$ B = array ('A', 'x', 'y ');
$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
Echo "\ r \ n case 3 \ r \ n ";
$ A = array (
1 => 'A ',
2 => 'B ',
3 => 'C ',
4 => 'D ',
5 => 'E ',
6 => 'F ');
$ B = array (
1 => 'A ',
7 => 'x ',
8 => 'y ');
$ C = array_merge ($ a, $ B );
Print_r ($ c );
$ C = $ a + $ B;
Print_r ($ c );
$ C = $ B + $;
Print_r ($ c );
?>

The result is as follows:Copy codeThe Code is as follows: first case
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 4
[4] => 5
[5] => 6
)
Case 2
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
[6] =>
[7] => x
[8] => y
)
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
)
Array
(
[0] =>
[1] => x
[2] => y
[3] => d
[4] => e
[5] => f
)
Case 3
Array
(
[0] =>
[1] => B
[2] => c
[3] => d
[4] => e
[5] => f
[6] =>
[7] => x
[8] => y
)
Array
(
[1] =>
[2] => B
[3] => c
[4] => d
[5] => e
[6] => f
[7] => x
[8] => y
)
Array
(
[1] =>
[7] => x
[8] => y
[2] => B
[3] => c
[4] => d
[5] => e
[6] => f
)

Related Article

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.