The difference between Array_merge function and Array+array in PHP

Source: Internet
Author: User

In PHP, you can use the Array_merge function and two arrays to add Array+array way to do the array merge, but the effect is not the same, the following is to introduce the specific use of the difference between the two.

The difference is as follows:

Array_merge () does not overwrite the original value when it is labeled as a value, but the Array+array merge array returns the first occurrence as the final result, and the values of the following array with the same key name are "discarded" (not overwritten).

When labeled as a character, Array+array still returns the first occurrence as the final result, and discards the values of the following array with the same key name, but Array_merge () overwrites the previous value of the same key name.

Example 1:

Code:

$arr 1 = [' PHP ', ' Apache '];
$arr 2 = [' PHP ', ' MySQl ', ' HTML ', ' CSS '];
$MERGEARR = Array_merge ($arr 1, $arr 2);
$PLUSARR = $arr 1 + $arr 2;
Var_dump ($MERGEARR);
Var_dump ($PLUSARR);

Results
$MERGEARR:

Array (size=6)
0 = String ' PHP ' (length=3)
1 = String ' Apache ' (length=5)
2 = String ' PHP ' (length=3)
3 = String ' MySQl ' (length=5)
4 = String ' HTML ' (length=4)
5 = String ' CSS ' (length=3)

$plusArr

Array (size=4)
0 = String ' PHP ' (length=3)
1 = String ' Apache ' (length=5)
2 = String ' HTML ' (length=4)
3 = String ' CSS ' (length=3

Example 2:

Code:

$arr 1 = [' PHP ', ' a ' = ' MySQl '];
$arr 2 = [' PHP ', ' MySQl ', ' a ' = ' + ' HTML ', ' CSS '];
$MERGEARR = Array_merge ($arr 1, $arr 2);
$PLUSARR = $arr 1 + $arr 2;
Var_dump ($MERGEARR);
Var_dump ($PLUSARR);

Results
$MERGEARR:

Array (size=5)
0 = String ' PHP ' (length=3)
' A ' = = String ' HTML ' (length=4)
1 = String ' PHP ' (length=3)
2 = String ' MySQl ' (length=5)
3 = String ' CSS ' (length=3)

$plusArr

Array (size=4)
0 = String ' PHP ' (length=3)
' A ' = = String ' MySQl ' (length=5)
1 = String ' MySQl ' (length=5)
2 = String ' CSS ' (length=3)

Example 3:

Code:

$arr 1 = [' PHP ', ' a ' = = ' MySQl ', ' 6 ' = ' CSS '];
$arr 2 = [' PHP ', ' MySQl ', ' a ' = ' + ' HTML ', ' CSS '];
$MERGEARR = Array_merge ($arr 1, $arr 2);
$PLUSARR = $arr 1 + $arr 2;
Var_dump ($MERGEARR);
Var_dump ($PLUSARR);

Results:

$mergeArr

Array (size=6)
0 = String ' PHP ' (length=3)
' A ' = = String ' HTML ' (length=4)
1 = String ' CSS ' (length=3)
2 = String ' PHP ' (length=3)
3 = String ' MySQl ' (length=5)
4 = String ' CSS ' (length=3)

$plusArr

Array (size=5)
0 = String ' PHP ' (length=3)
' A ' = = String ' MySQl ' (length=5)
6 = String ' CSS ' (length=3)
1 = String ' MySQl ' (length=5)
2 = String ' CSS ' (length=3)

The difference between Array_merge function and Array+array in PHP

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.