I often talk about the PHP Array Function array_merge (mandatory), an empty array of arraymerge

Source: Internet
Author: User

I often talk about the PHP Array Function array_merge (mandatory), an empty array of arraymerge

I used this function a long time ago. It's not just a simple application, but it's not very in-depth research.

Today, I read some of my usage experiences on array_merge from other blogs, so I will summarize it myself.

 

Array_merge combines one or more arrays.

This function is mostly used for merging the result set retrieved from the database.

The parameter configuration is also simple. array_merge (arr1, arr2, arrN)

Note that the parameters here must be arrays; otherwise, an error is returned.

Although it looks simple, there are also many pitfalls.

We will analyze the data in single array and multi-array directions.

 

1. Merge multiple arrays (custom key names ):

The running result below shows that the data with the same name in $ arr1 and $ arr2 has been overwritten.

The following array overwrites the same values in the preceding array.

<?php$arr1 = array('a'=>'1','b'=>'2');$arr2 = array('a'=>'1','b'=>'3','c'=>'2');$ret = array_merge($arr1,$arr2);print_r($ret);//run resultArray(  [a] => 1  [b] => 3  [c] => 2)

2. Merge multiple arrays (custom key names ):

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

<?php$arr1 = array(1=>'1',2=>'2');$arr2 = array(1=>'1',2=>'3',6=>'2');$ret = array_merge($arr1,$arr2);print_r($ret);//run resultArray(  [0] => 1  [1] => 2  [2] => 1  [3] => 3  [4] => 2)

3. Merge multiple arrays (no input key name ):

<?php$arr1 = array(1,2);$arr2 = array(1,2,6);$ret = array_merge($arr1,$arr2);print_r($ret);//run resultArray(  [0] => 1  [1] => 2  [2] => 1  [3] => 2  [4] => 6)

4. Most merge operations (when an array is empty ):

This operation is very common when an array is empty. No matter which array is empty, the existing value is displayed.

<?php$arr1 = array();$arr2 = array(1,2,6);$ret = array_merge($arr1,$arr2);print_r($ret);//run resultArray(  [0] => 1  [1] => 2  [2] => 6)

5. Single Array Operation:

Array_merge () is generally not used for single array, because this function is used to merge arrays.

However, you can use this function if you want to restore the original key name, but it is not recommended to do so.

Because there are better functions than it can use array_values ()

The single array operation is actually very similar to the two cases above. When the key name is a number, the key name will be formatted; otherwise, it will be displayed directly.

<?php$arr1 = array(1=>1,3=>2,6=>6);$ret = array_merge($arr1);print_r($ret);//run resultArray(  [0] => 1  [1] => 2  [2] => 6)

6. When two arrays are merged, array_merge () can be used. The following arrays are merged with the preceding arrays.

But what should I do when I want to combine the array above with the array below?

Do you want to change the positions of the two arrays in array_merge? There is actually a simpler method.

Is to use "+" to complete the operation.

<?php$arr1 = array('a'=>1,'b'=>2);$arr2 = array('a'=>1,'b'=>3,'c'=>6);$ret = $arr1+$arr2;print_r($ret);//run resultArray(  [a] => 1  [b] => 2  [c] => 6)

In this article, we often talk about the PHP Array Function array_merge (which is a must-read Article). I hope you can give us a reference and support for our guests.

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.