Differences between the PHP function array_merge () and the plus sign operator _ PHP Tutorial

Source: Internet
Author: User
The difference between the PHP function array_merge () and the plus sign operator. We will introduce array_merge in the reference manual as follows: the PHP function array_merge () combines two or more arrays, the value in an array is appended. what we will introduce today isArray_merge is described in the reference manual as follows:

The PHP function array_merge () combines two or more arrays. the values in an array are appended to the values of the previous array. Returns an array of results.

If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.

The difference between the PHP function array_merge () and the plus sign operator is:

1. when the array KEY name is a numeric KEY, when the two arrays to be merged have a numeric KEY with the same name, using array_merge () will not overwrite the original value, when "+" is used to merge arrays, the first value will be returned as the final result, and the values of the following arrays with the same key name will be discarded (note: instead of overwriting, the value that appears first is retained ). Example:

 
 
  1. $ArrayArray1= Array (1=>'0 ');
  2. $ArrayArray2= Array (1=>"Data ");
  3. $Result1= $ Array2 + $ array1;/* The result is the value of $ array2 */
  4. Print_r ($ result );
  5. $Result= $ Array1 + $ array2;/* The result is a value of $ array1 */
  6. Print_r ($ result );
  7. $Result3=Array_merge($ Array2, $ array1);/* The result is the values of $ array2 and $ array1. The key name is reassigned */
  8. Print_r ($ result3 );
  9. $Result4=Array_merge($ Array1, $ array2);/* The result is the values of $ array1 and $ array2. The key name is reassigned */
  10. Print_r ($ result4 );

Output result:

 
 
  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 is a number, but the PHP function array_merge () will overwrite the value of the previous same key name.

Example:

 
 
  1. $ArrayArray1= Array ('asd '=>'0 ');
  2. $ArrayArray2= Array ('asd '=>"Data ");
  3. $Result1= $ Array2 + $ array1;/* The result is the value of $ array2 */
  4. Print_r ($ result );
  5. $Result= $ Array1 + $ array2;/* The result is a value of $ array1 */
  6. Print_r ($ result );
  7. $Result3=Array_merge($ Array2, $ array1);/* The result is $ array1 */
  8. Print_r ($ result3 );
  9. $Result4=Array_merge($ Array1, $ array2);/* The result is $ array2 */
  10. Print_r ($ result4 );

Output result:

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

The above is the difference between the array_merge () and the plus sign operator of the PHP function in actual use.


The description of javasarray_merge in the reference manual is as follows: the PHP function array_merge () combines two or more arrays, and the values in an array are appended...

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.