In-depth analysis of the differences between using the plus sign in php and merging arrays with array_merge

Source: Internet
Author: User
This article gives a detailed analysis of the differences between using the plus sign in php and merging arrays with array_merge. For more information, see the following.
The code is as follows:
$ R = array (1, 2, 3, 4, 5, 6 );
$ E = array (7,8, 9, 10 );
?>

Here we use the array_merge and the plus sign to add these two arrays.
The code is as follows:
Print_r ($ r + e ); // output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6)
Print"
";
Print_r (array_merge ($ r, $ e )); // output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)
?>

It can be seen from this that the value in an array is combined with array_merge and appended to the value behind the previous array. If the array contains the number key name, the subsequent values will not overwrite the original values, but will be appended to the back. However, the plus sign is used to merge arrays. if the key name is the same, the first array value is used.
Next we will change the array given above
The code is as follows:
$ R = array ('R' => 1, 2, 3, 4, 5, 6 );
$ E = array ('R' => 7, 8, 9, 10 );
?>

The code is as follows:
Print_r ($ r + e ); // output Array ([r] => 1 [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6)
Print"
";
Print_r (array_merge ($ r, $ e )); // output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)
?>

It can be seen from this that the value in an array is combined with array_merge and appended to the value behind the previous array. If the names of non-numeric keys are the same, the values of the subsequent arrays overwrite the values of the preceding arrays. However, the plus sign is used to merge arrays. if the key name is the same, the first array value is used.

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.