Home >Backend Development >PHP Problem >How to merge arrays in PHP array operation? Summary of common methods
In the previous article "PHP Array Operations Matching Search Elements and Key Names in Arrays" we introduced in detail the relevant knowledge of search matching for array operations in PHP. In this article we will continue Let’s take a look at some common operations of array merging in PHP. I hope everyone has to help!
In the previous article we introduced the in_array
function, array_search
function and for array operations in PHP The array_key_exists
function can be used to search and match elements in an array. Next, we can continue to look at some commonly used function operations related to arrays in PHP development work.
Let's introduce how to complete the merging of arrays in PHP. To achieve such an operation, we need to use the array_merge
function and the array_merge_recursive
function. Below we will introduce these two functions separately.
array_merge
() function - overwrites the previous array elements with the same key name
In PHP we can passarray_merge
() function is used to merge arrays, that is, to merge elements in multiple arrays into one array. The basic syntax format of the array_merge
function is as follows:
array_merge(array1,array2,array3...)
What needs to be noted is: The parameter array123 is the array used for merging. This function can merge multiple arrays. If two elements or multiple elements have the same key name, after merging, the last element with their key name will overwrite the other elements.
This function can also merge an array. Yes, how do you say merging an array? What I mean at this time is that if the function inputs only one array, and the key name of this array is an integer, When "merged" by this function, the key names of the new array output are re-indexed starting from 0
.
Next, let’s take a brief look at the use of the array_merge
function through an example. The example is as follows:
<?php $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); print_r(array_merge($a1,$a2)); ?>
Output result:
In the above example, both arrays to be merged have elements with the key name "b". You can see the last element with the key name "b" in the final output result. "yellow" overwrites the previous elements, so there are only three elements in the final output.
Let’s take a look at the operation of “merging” an array. The example is as follows:
<?php $a=array(3=>"red",4=>"green"); print_r(array_merge($a)); ?>
Output result:
In the above example, in an array that needs to be "merged", the key names of the array are all integers and are not indexed from 0. After merging through the array_merge function, the key names of the array change and are indexed from 0.
<strong><span style="font-size: 20px;">array_merge_recursive</span></strong>
##Function - will not overwrite array elements with the same key name
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 know is:
array_mergeAfter the function is merged, the element whose key name is the last will overwrite other elements; however, the
array_merge_recursive function can recurse elements with the same key name into an array without overwriting them.
<?php $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); print_r(array_merge($a1,$a2)); echo '<br/>'; print_r(array_merge_recursive($a1,$a2)); ?>Output result:
array_merge function, the same element key name will be overwritten. The key names of elements merged through the
array_merge_recursive function will not be overwritten.
0 just like the array_merge function.
<?php $a=array(3=>"red",4=>"green"); print_r(array_merge_recursive($a)); ?>Output result:
##It can be seen from the above example
array_merge_recursive## The main difference between #function andarray_merge function is whether elements with the same key name will be overwritten.
<strong><span style="font-size: 20px;">+</span></strong>
合并数组-覆盖后面相同键名的数组元素
通过+
来进行数组的合并可以说是最简单的一种数组合并方法了,让我们直接通过示例来看一下用法,示例如下:
<?php $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); $a3=$a1+$a2; print_r($a3); ?>
输出结果:
由上述示例能够看出,通过+的方式来合并数组,和通过array_merge函数的方式来合并数组的区别就在于:
array_merge
函数遇到相同键名的不同数组元素,合并之后会被覆盖掉,被覆盖的是前面的数组元素;
而+
遇到相同键名的不同数组元素进行合并,合并之后也会被覆盖掉,但是覆盖的元素是后面的数组元素。
<strong><span style="font-size: 20px;">array_combine</span></strong>
函数-一组做键名一组做键值
在PHP中还有一个函数是array_combine
函数,它能够将两个数组进行合并,并且其中一个数组的元素是合并后新数组的键名,另一个数组中的元素是合并后新数组的键值。
array_combine
函数的基本语法格式如下:
array_combine(keys,values);
其中需要注意的是,参数keys
表示的就是作为键名的数组,参数value
表示的就是作为键值的数组,这两个数组中的元素个数一定要相同,也就是两个数组合并之后需要每个键名都有相对应的键值。
通过array_combine
函数合并成功的话,返回的结果是合并成功后的数组,如果两个数组中的元素个数不相同那么返回的结果就是flase。
下面我们通过示例来看一下array_combine
函数的应用,示例如下:
<?php $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); $a3 = array_combine($a1, $a2); print_r($a3); ?>
输出结果:
由此我们便通过array_combine函数完成了两个数组的合并,并且其中一个数组作为键名,另一个数组作为键值。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of How to merge arrays in PHP array operation? Summary of common methods. For more information, please follow other related articles on the PHP Chinese website!