php function Array_merge () differs from the plus operator _php Tutorial

Source: Internet
Author: User
What we are introducing to you today isThe Array_merge is described in the reference manual as follows:

The PHP function Array_merge () merges two or more arrays of cells, 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.

The difference between the PHP function Array_merge () and the plus operator two is:

1. When the array key name is a numeric key name, the two arrays to be merged have the same name as the number key, using Array_merge () will not overwrite the original value, while using "+" to merge the array will return the first occurrence of the value as the final result, and the following array with the same key name of those values "discard" (note: Instead of overwriting, it retains the first value that appears). Example:

 
 
  1. $ Array array1 = Array (1=>' 0 ');
  2. $ Array array2 = Array (1=> "Data");
  3. $ RESULT1 = $array 2 + $array 1;/* result is the value of $array2 * /
  4. Print_r ($result);
  5. $ result = $array 1 + $array 2;/* The result is a value of $array1 * /
  6. Print_r ($result);
  7. $ RESULT3 = Array_merge ($array 2, $array 1);/* The result is a value of $array2 and $array1, and the key name is reassigned */
  8. Print_r ($result 3);
  9. $ RESULT4 = Array_merge ($array 1, $array 2);/* The result is a value of $array1 and $array2, and the key name is reassigned */
  10. Print_r ($result 4);

The output is:

 
  
  
  1. Array ([1] => data)
  2. Array ([1] => 0)
  3. Array (
  4. [0] => data
  5. [1] => 0
  6. )
  7. Array
  8. (
  9. [0] => 0
  10. [1] => data
  11. )

2. When the same array key name is a character, the "+" operator is the same as the key name, but the PHP function Array_merge () overrides the previous value of the same key name.

Example:

 
 
  1. $ Array array1 = Array (' ASD ' =>' 0 ');
  2. $ Array array2 = Array (' ASD ' => "Data");
  3. $ RESULT1 = $array 2 + $array 1;/* result is the value of $array2 * /
  4. Print_r ($result);
  5. $ result = $array 1 + $array 2;/* The result is a value of $array1 * /
  6. Print_r ($result);
  7. $ RESULT3 = Array_merge ($array 2, $array 1);/* results are $array1*/
  8. Print_r ($result 3);
  9. $ RESULT4 = Array_merge ($array 1, $array 2);/* results are $array2*/
  10. Print_r ($result 4);

The output is:

 
  
  
  1. Array ([ASD] => data)
  2. Array ([ASD] => 0)
  3. Array ([ASD] => 0)
  4. Array ([ASD] => data)

These are the differences between the PHP function Array_merge () and the plus operator in actual use.


http://www.bkjia.com/PHPjc/446400.html www.bkjia.com true http://www.bkjia.com/PHPjc/446400.html techarticle What we are introducing to you today is array_merge in the reference manual as follows: the PHP function Array_merge () merges two or more arrays of cells, and the values in an array are appended to ...

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