PHP Merge Arrays Use the + operator and array function array_merge the difference instance

Source: Internet
Author: User
PHP merging arrays generally have two practices, one is to add directly using the plus sign, the other is to use the Array_merge function to add, Array_merge () to merge two or more arrays of cells, an array of values 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.

There is a difference between the two:

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 as the final result, and the values of the following array with the same key name "Discard" (note: Instead of overwriting, it retains the first value that appears). When the same array key name is a character, the "+" operator is the same as the key name, but Array_merge () overrides the previous value of the same key name.

Instance:

<?php$array1 = Array (1=> ' 0 '); $array 2 = Array (1=> "data"); $result 1 = $array 2 + $array 1;/* result is the value of $array2 */Print_r ($result); $result = $array 1 + $array 2;/* The result is the value of $array1 */Print_r ($result); $result 3 = Array_merge ($array 2, $array 1);/* results are $array2 and $array1 values, key names are reassigned */Print_r ($result 3); $result 4 = Array_merge ($array 1, $array 2);/* results are $array1 and $array2 values, key names are reassigned */Print_r ($result 4);?>

Output Result:

Array ([1] = data) array ([1] = 0) Array ([0] = + data [1] + 0) Array ([0] = 0 [1] = data)

Code:

<?php$array1 = Array (' asd ' = ' 0 '); $array 2 = Array (' ASD ' = ' data '); $result 1 = $array 2 + $array 1;/* result is the value of $array2 */Print_r ($result); $result = $array 1 + $array 2;/* The result is the value of $array1 */Print_r ($result); $result 3 = Array_merge ($array 2, $array 1);/* The result is $array1*/Print_r ($result 3); $result 4 = Array_merge ($array 1, $array 2);/* Results for $array2*/Print_r ($result 4);? >

Output Result:

Array ([ASD] = data) array ([ASD] = 0) Array ([ASD] + 0) Array ([ASD] + data)

1. The natural index in the array will not be reset
2. In the Add method, the values in the added array are not overwritten
The natural index in the 3.merge function is reset
The 4.merge function, which does not matter by the merge and merge relationships, the more the following array parameter, its value, overrides the value of the same key as the previous array parameter

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.