How to merge arrays in PHP? To do this we need to use the array_merge function and the array_merge_recursive function. Here we will introduce these two functions separately.
array_merge() function - overwrites the previous array elements with the same key name
In PHP we can use the array_merge() function to merge arrays, that is, to merge elements from multiple arrays into one array. The basic syntax format of the array_merge function is as follows.
1 |
|
Among the things to note are:The parameter array123 is the array to be merged. This function can merge multiple arrays, where if two elements or more elements have the same key name, the element whose key name is the last element will overwrite the other elements after merging.
Through the function can also merge an array, yes, an array how to say merge it, it is said that if the function input only an array, and the key name of this array is an integer, when the function "merge" after the output of the new array its key name is reindexed from 0.
Next, let's take a brief look at the use of the array_merge function with the following example:
1 2 3 4 5 |
|
In the above example, the two arrays to be merged both have elements with key name "b", and you can see that the last element with key name "b" "yellow " overwrites the previous element, so there are only three elements in the final output.
Let's look at the operation of "merging" an array, the example is as follows:
1 2 3 4 |
|
In the above example, in one of the arrays to be "merged", the key names of the array are integers and not indexed from 0. When merged by array_merge function, the key names of the array are changed to be indexed from 0.
array_merge_recursive function - does not overwrite array elements with the same key name
In PHP not only array_merge function can merge arrays, what you need to know is that array_merge_recursive function can also merge one or more arrays into one array, there is not much difference between the two, the difference you need to know is:
When two arrays that need to be merged have elements with the same key name, the two functions are handled differently. The example above also says that after the array_merge function merges their key name is the last element that will overwrite the other elements; but the array_merge_recursive function recurses the same key name elements into one array, and does not overwrite them.
Let's look at it with the example above, which is as follows:
1 2 3 4 5 6 7 |
|
The above example shows the difference between the two functions: it should be noted that the same two arrays are merged by different functions, and the output results are different.
Also, if you "merge" an array with the array_merge_recursive function, the result will be reindexed from 0, just like the array_merge function.
Examples are as follows:
1 2 3 4 |
|
The main difference between the array_merge_recursive function and the array_merge function is that they do not overwrite elements with the same key name, as can be seen from the above example.
Merge arrays - overwrite elements of arrays with the same key name behind them
Merging arrays by + is arguably one of the easiest ways to merge arrays, so let's take a look at the usage directly from the example, which is as follows:
1 2 3 4 5 6 |
|
As you can see from the above example, the difference between merging arrays by means of + and merging arrays by means of the array_merge function is that:
-
array_merge function encounters different array elements with the same key name, which are merged and then overwritten with the previous array elements.
-
And + when different array elements with the same key name are encountered for merging, they will also be overwritten after merging, but the overwritten elements are the later array elements.
array_combine function - one set of key names and one set of key values
Another function in PHP is the array_combine function, which can merge two arrays, and the elements of one array are the key names of the new merged array, and the elements of the other array are the key values of the new merged array.
The basic syntax format of the array_combine function is as follows
:
1 |
|
It should be noted that the parameter keys is the array of key names, and the parameter value is the array of key values, the number of elements in these two arrays must be the same, that is, after the merging of the two arrays, each key name must have the corresponding key value.
If the array_combine function succeeds, the result returned is the successfully merged array, if the number of elements in the two arrays is not the same, then the result returned is flase.
Let's look at the application of the array_combine function with the following example:
1 2 3 4 5 6 |
|
This allows us to merge two arrays by using the array_combine function, with one array as the key name and the other array as the key value.