Two ways to merge PHP arrays _php tutorial

Source: Internet
Author: User
Let's start with a two array
Copy CodeThe code is as follows:
$r = Array (1,2,3,4,5,6);
$e = Array (7,8,9,10);
?>

Now let's use the Array_merge and the plus sign to why these two arrays
Copy the Code code 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 here that the values in an array with Array_merge are appended to the previous array. Returns an array that is the result of if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back. However, using the plus sign to merge the array if the key name is the same, take the first occurrence of the array value, followed by the directly ignored

Let's change the array given in the previous
Copy the Code code as follows:
$r = Array (' R ' =>1,2,3,4,5,6);
$e = Array (' r '= = 7,8,9,10);
?>

Copy the Code code 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 here that the values in an array with Array_merge are appended to the previous array. If the non-numeric key name is the same, the value of the subsequent array overrides the value of the preceding array. However, using the plus sign to merge the array if the key name is the same, take the first occurrence of the array value, followed by the directly ignored

http://www.bkjia.com/PHPjc/744324.html www.bkjia.com true http://www.bkjia.com/PHPjc/744324.html techarticle We first give two array copy code code as follows: PHP $r = Array (1,2,3,4,5,6), $e = Array (7,8,9,10), the following we use Array_merge and the plus sign to why these two arrays copy code ...

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