PHP inside merge array Array_merge and plus what's the difference

Source: Internet
Author: User
PHP looks like both of these methods can be combined array, I found a point, is array_mergeIs behind the cover of the front, and +Is the previous overlay, such as the following code

$a = Array (' a ' = = 1, ' b ' = = 2, ' c ' = = 3); $b = Array (' b ' = = 5, ' C ' + 6, ' d ' + 7);p Rint_r (Array_merge ($a , $b));p Rint_r ($a + $b);

On the output

Array (    [A] = 1    [b] = 5    [c] = 6    [d] + 7) Array (    [a] + = 1    [b] = 2    [c] => ; 3    [d] = 7)

Has anyone summed up the difference between the two?

Reply content:

PHP looks like both of these methods can be combined array, I found a point, is the array_merge back cover the front, but + the front cover behind, such as the following code

$a = Array (' a ' = = 1, ' b ' = = 2, ' c ' = = 3); $b = Array (' b ' = = 5, ' C ' + 6, ' d ' + 7);p Rint_r (Array_merge ($a , $b));p Rint_r ($a + $b);

On the output

Array (    [A] = 1    [b] = 5    [c] = 6    [d] + 7) Array (    [a] + = 1    [b] = 2    [c] => ; 3    [d] = 7)

Has anyone summed up the difference between the two?

Array_merge ():
If the input array has the same string key name, the value following the key name overrides the previous value. (your 1567) if the array contains a numeric key name (you can try it and toss me before), the value behind will not overwrite the original value, but append it to the back. (The numeric key name is reassigned and will always become zero-based.) )

For merging arrays with "+":
If the array has the same string key name (whether it is not a number), the first occurrence is returned as the final result, and the values of the following array with the same key name are "discarded". (Your 1237)

A lot of people have summed up, when this problem is Google God rescued.

In fact, the handbook is very clear:

Array Array_merge (array $array 1 [, Array $array 2 [, array $ ...])

Array_merge () Merges the cells of one or more arrays, and the values in an array are appended to the previous array. Returns an array as the result.

If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.

If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.

First of all, my PHP version is 5.3.3

var_dump(array_merge(array('key'=>'value'),array(-1=>'-1',0=>'0')));

Get:

That is, although two arrays are given, one of which is not an indexed array, Array_emrge will still index the index array again. However, "+" will not.

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