I used this function a long time ago, but it was just a simple use and didn't do much in-depth research.
Today, when I was browsing through other people's blogs, I saw some experience in using array_merge, so I will summarize it myself.
array_merge is to merge one or more arrays.
This function is mostly used to merge the result sets taken from the database.
Parameter configuration is also very simple array_merge(arr1,arr2,arrN)
Note that the parameters here must be an array, otherwise an error will be reported.
Although it seems simple, there are many pitfalls.
We will analyze from the direction of single and multi-arrays.
1. Multiple arrays are merged (custom key names):
From the following run results, we can see that the data with the same key name as $arr1 and $arr2 have been overwritten.
The following array will overwrite the same value as the key in the previous 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 result Array ( [a] => 1 [b] => 3 [c] => 2 )
2. Multiple arrays are merged (custom numeric key names):
If the key name in the array is a number, the key name is formatted and all key values are preserved.
<?php $arr1 = array(1=>'1',2=>'2'); $arr2 = array(1=>'1',2=>'3',6=>'2'); $ret = array_merge($arr1,$arr2); print_r($ret); //run result Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 3 [4] => 2 )
3. Multiple arrays are merged (no key names are entered):
<?php $arr1 = array(1,2); $arr2 = array(1,2,6); $ret = array_merge($arr1,$arr2); print_r($ret); //run result Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 6 )
4. Most are in merge operations (when an array is empty):
This operation is common for an array to be empty, and any array to be empty will display the existing value.
<?php $arr1 = array(); $arr2 = array(1,2,6); $ret = array_merge($arr1,$arr2); print_r($ret); //run result Array ( [0] => 1 [1] => 2 [2] => 6 )
5. Single array operation:
In this case of single arrays, array_merge() is not generally used, because this function is used to merge arrays.
However, if you want to restore to the original critical name, you can use this function, but it is not recommended to do so.
Because there are better functions than it can use array_values()
Single array operation is actually very similar to the above two situations. 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 result Array ( [0] => 1 [1] => 2 [2] => 6 )
6. When two arrays are combined, you can use array_merge(). The subsequent array will be merged with the previous array.
But what should I do when I want to merge the previous array with the following array?
Should I change the position of the two arrays in array_merge()? There is actually an easier way.
It 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 result Array ( [a] => 1 [b] => 2 [c] => 6 )
The above article is a cliché about PHP array function array_merge (must-read) which is all the content I share with you. I hope you can give you a reference and I hope you can support me more.