Two methods and differences of array merging in PHP

Source: Internet
Author: User
The two methods and differences of array merging in PHP. if you need them, you can refer to the two methods and differences of PHP array merging.
If it is an associated array, as follows:
The code is as follows:
$ A = array (
'Where' => 'uid = 1 ',
'Order' => 'uid ',
);
$ B = array (
'Where' => 'uid = 2 ',
'Order' => 'uid desc ',
);


1. array_merge. if the two arrays have the same key, the next one will overwrite the previous one.
The code is as follows:
$ C = array_merge ($ a, $ B );
Var_export ($ c); // The result is the same as the original $ B.
$ D = array_merge ($ B, $ );
Var_export ($ d); // The result is the same as the original $.

2. "+" operator. if two arrays have the same key, the previous one will overwrite
The code is as follows:
$ C = $ a + $ B;
Var_export ($ c); // The result is the same as the original $.
$ D = $ B + $;
Var_export ($ d); // The result is the same as the original $ B.


If it is a numeric index array, as follows:
The code is as follows:
$ A = array (
1 => '123 ',
2 => '123'
);
$ B = array (
4 => '123 ',
1 => '123'
);

1. array_merge. The effect is similar to the code foreach. each array element is pushed into a new stack.
The code is as follows:
$ C = array_merge ($ a, $ B );
Var_export ($ c );
$ D = array_merge ($ B, $ );
Var_export ($ d );

Output:
Array (
0 => '123 ',
1 => '123 ',
2 => '123 ',
3 => '123 ',
)
Array (
0 => '123 ',
1 => '123 ',
2 => '123 ',
3 => '123 ',
)
2. "+" operator. The effect is similar to the code foreach. each array element is pushed into a new stack. if the same key already exists, it is not processed.
The code is as follows:
$ C = $ a + $ B;
Var_export ($ c );
$ D = $ B + $;
Var_export ($ d );

Output:
Array (
1 => '123 ',
2 => '123 ',
4 => '123 ',
)
Array (
4 => '123 ',
1 => '123 ',
2 => '123 ',
)
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.