Two ways to merge PHP arrays

Source: Internet
Author: User

  This article mainly introduces two methods of the PHP array merging, and also tells the difference between using the plus sign and using the Array_merge to merge the array, the need friend can refer to the following

We first give two arrays   code as follows: <?php     $r = array (1,2,3,4,5,6);     $e = array (7,8,9,10);  ?>      We're going to use the Array_merge and the plus sign here. The two arrays     code are as follows: <?php   Print_r ($r +e); Output <span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:16PX; ">array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6) </span>   print" <br /> ";   Print_r (Array_merge ($r, $e)); Output <span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:16PX; ">array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9) </span>  ?>      From here you can see that the value in an array of Array_merge merged arrays is appended to the back of the previous array. Returns an array as a result if the array contains a numeric key name, the subsequent value will not overwrite the original value, but append to the back. However, with the plus sign to merge the array if the key name is the same, then the first occurrence of the array value, the following directly ignore the   below we'll change the array     code as follows: <?php     $r = Array (' r ' = >1,2,3,4,5,6);     $e = Array (<span style= "Background-color:rgb" (245, 250, 255); "> ' R ' =></span>7,8,9,10);  ?>       code is as follows: <?php   Print_r ($r +e); Output array ([r] => 1 [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6)   print "<br/>";   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)  ?>     From here you can see that the value in an array of Array_merge merged arrays is appended to the back of the previous array. If the name of the Non-numeric key is the same, the value of the following array overrides the value of the preceding array. However, with the plus sign to merge the array if the key name is the same, then the first occurrence of the array value, the following is directly ignored

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.