Parse the differences between array_merge and array + array in php

Source: Internet
Author: User
This article provides a detailed analysis of the differences between array_merge and array + array in php. For more information, see

This article provides a detailed analysis of the differences between array_merge and array + array in php. For more information, see

Array_merge discards the key of the original number, retains the key in the string form, and then forms a new array. no matter whether the key name is the same, the Hong Kong server will not merge, unless the key name and value are the same and must also be a string type key, the key is merged. Array + array is no matter what the situation is, it will only put the data in the previous array first into the newly generated array, then, check whether the second array has more numbers than the first array. If there are more, add them to the Hong Kong virtual host. The number is only a number of Hong Kong servers. However, in this case, the second array will be added: $ a = array ('D' => 'aass ', 'E' => 'adsdfs', 'asd' => 'asdsdd ', 'ddfg' => 'dssdf ');

The Code is as follows:


$ B = array ('D' => 'adddd', 'adsdfs', 'asdfsdddd', 'D' => 'aass ');
$ D = $ a + $ B;
$ E = array_merge ($ a, $ B );
Var_dump ($ d );
Var_dump ($ e );


Print:
Array
'D' => string 'aass '(length = 4)
'E' => string 'adsdfs' (length = 6)
'Asd '=> string 'asdsdd' (length = 6)
'Ddfg' => string 'dssdf '(length = 5)
0 => string 'adsdfs' (length = 6)
1 => string 'asdfsddddd' (length = 10)
Array
'D' => string 'aass '(length = 4)
'E' => string 'adsdfs' (length = 6)
'Asd '=> string 'asdsdd' (length = 6)
'Ddfg' => string 'dssdf '(length = 5)
0 => string 'adsdfs' (length = 6)
1 => string 'asdfsddddd' (length = 10)

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.