Differences between array_merge and array + array in php

Source: Internet
Author: User
Differences between array_merge and array + array in php
This article introduces how to merge arrays using array_merge and how to merge arrays with arrays. For more information, see.

Note: In php, array_merge discards the key of the original number and retains the key in the string format to form a new array. Keys are not merged regardless of the key name, keys in string format are merged unless the key name and value are the same. But array + array is different. it only places the data in the previous array in the new array first, and then checks whether the second array has more than the first array, if there is more than one, it is only a number, but in this case it will be added. This is the difference between the two.

Let's look at the following example:

     'Aass', 'E' => 'adsdfs', 'asd '=> 'asdsdd', 'ddfg' => 'dssdf '); $ B = array ('D' => 'adddd', 'adsdfs', 'asdfsdddd', 'D' => 'aass '); $ d = $ a + $ B; $ e = array_merge ($ a, $ B); var_dump ($ d); var_dump ($ e);?>

Output result:

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 'asdfsdddddd' (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 'adsdf' (length = 6) 1 => string 'asdfsdddddd' (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.