PHP Array function Array_merge

Source: Internet
Author: User
This article mainly introduces the PHP array function array_merge, the interested friend's reference, hoped to be helpful to everybody.

This function has been used for a long time, but it's not just a simple use, it's not a very thorough study.

Today, while flipping through other people's blogs, I saw some use of array_merge, so I have a summary.

Array_merge is the merging of one or more arrays.

This function is useful for merging operations on result sets that are taken out of the database.

Parameter configuration is also simple Array_merge (ARR1,ARR2,ARRN)

Note that the parameters here must be an array, otherwise an error will be presented.

Although, it seems very simple, but there are a lot of pits.

We will analyze the direction of the singular and the majority of the groups.

1. Multiple arrays for merging operations (custom key names):

From the running results below, it can be seen that the $ARR1 and $ARR2 have the same data as the key name is overwritten.

The subsequent array overrides the same value as the key in the previous array.

<?PHP$ARR1 = Array (' A ' = ' 1 ', ' b ' = ' 2 '); $arr 2 = Array (' a ' = = ' 1 ', ' b ' = ' 3 ', ' c ' = ' 2 '); $ret = Array_merge ($arr 1, $arr 2);p Rint_r ($ret);//run resultarray (  [A] = 1  [b] = 3  [c] + 2)

2. Multiple arrays for merging operations (custom numeric key names):

If the key name in the array is a number, the key name is formatted and all the key values are preserved.

<?PHP$ARR1 = Array (1=> ' 1 ',2=> ' 2 '); $arr 2 = Array (1=> ' 1 ',2=> ' 3 ',6=> ' 2 '); $ret = Array_merge ($arr 1,$ ARR2);p Rint_r ($ret);//run resultarray (  [0] = 1  [1] = 2  [2] = 1  [3] = 3  [4] = 2)

3. Multiple arrays for merging operations (no input key name):

<?PHP$ARR1 = Array, $arr 2 = Array (1,2,6), $ret = Array_merge ($arr 1, $arr 2);p Rint_r ($ret);//run resultarray (  [0] = 1  [1] = 2  [2] = 1  [3] = 2  [4] = 6)

4. Most in a merge operation (when an array is empty):

It is common for an array to be empty, and the existing value is displayed regardless of which array is empty.

<?PHP$ARR1 = Array (), $arr 2 = Array (1,2,6), $ret = Array_merge ($arr 1, $arr 2);p Rint_r ($ret);//run resultarray (  [0] = 1  [1] = 2  [2] = 6)

5. Singular group operation:

Singular group This situation generally does not use Array_merge (), because this function is used to merge arrays.

However, this function can be used if you want to revert to the original correlation key name, but it is not recommended.

Because there is a better function than it can use Array_values ()

The singular group operation is similar to the above 2 or 32 cases, when the key name is a number, the key name is formatted, otherwise it is displayed directly.

<?PHP$ARR1 = Array (1=>1,3=>2,6=>6), $ret = Array_merge ($arr 1);p rint_r ($ret);//run resultarray (  [0] = > 1  [1] = 2  [2] = 6)

6. You can use Array_merge () when encountering two array merges, and the subsequent array will be merged with the previous array

But what if you want to use the previous array to merge with the following array?

Would you like to change the position of the two arrays in Array_merge ()? There is actually an easier way.

is to use "+" to complete the operation.

<?PHP$ARR1 = Array (' A ' =>1, ' B ' =>2); $arr 2 = Array (' A ' =>1, ' B ' =>3, ' C ' =>6); $ret = $arr 1+ $arr 2;print_r ($ RET);//run resultarray (  [A] = 1  [b] = 2  [c] = 6)

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.