Home>Article>Backend Development> Detailed explanation of 2 methods of merging arrays in PHP
Everyone knows that two arrays in PHP can be combined or array_merge, but there are still differences between them, and if these differences are not understood clearly, it will be fatal in the project! This article summarizes and introduces two methods of merging arrays in PHP. Friends in need can refer to it. Let's learn together.
Preface
Before merging arrays, I have been using thearray_merge()
function, but recently I When I changed jobs, I encountered an interview question about merging arrays. What I was thinking was to convert the two arrays into strings first, and then merge them into arrays for output. The interviewer said that this idea was not right, so Bulabula talked about it. Array-based things, and it is really due to experience issues, or too little code writing, I can't think of any other methods. Today I searched on Baidu, and it turns out there is a ' ' sign,
array_merge_recursive()
, can also be used to merge arrays. According to my memory, I will write out the question and take a look:
$a = array('color'=>'red',5,6); $b = array('color'=>'blue','type'=>'fruit',6,7); $arr = array_merge($a,$b); var_dump($arr);
array (size=6) 'color' => string 'blue' (length=4) 0 => int 5 1 => int 6 'type' => string 'fruit' (length=5) 2 => int 6 3 => int 7
The requirement is to get the same effect without usingarray_merge()
;
(array_merge()
merge The array will overwrite the key values of the associated form array in the previous array with the same key values, and the index form will be merged together in the order of key values)
1. First use the array_merge_recursive() function to merge For a moment:
$a = array('color'=>'red',5,6); $b = array('color'=>'blue','type'=>'fruit',6,7); $arr = array_merge_recursive($a,$b); var_dump($arr);
Output result:
array (size=6) 'color' => array (size=2) 0 => string 'red' (length=3) 1 => string 'blue' (length=4) 0 => int 5 1 => int 6 'type' => string 'fruit' (length=5) 2 => int 6 3 => int 7
From The result can be seen thatarray_merge_recursive()
The function will return values with the same key value in the form of a new associative array, and use this key value as the key value of the two-dimensional array. Other index forms will not Affected.
Compared witharray_merge()
, there will be no situation where the subsequent array key is the same as the previous one and the previous value will be overwritten.
2. Let’s look at the situation of merging arrays with number ' ':
$a = array('color'=>'red',5,6); $b = array('color'=>'blue','type'=>'fruit',6,7); $arr = $a+$b; var_dump($arr);
Output result:
array (size=4) 'color' => string 'red' (length=3) 0 => int 5 1 => int 6 'type' => string 'fruit' (length=5)
It can be seen from this result that using the ' ' sign to merge arrays covers the front and back, andarray_merge()
On the contrary, and it is more ruthless thanarray_merge()
. If the contents of the array appear in index form, they will be overwritten if the key values are the same after merging!
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP’s method of implementing polymorphic applications based on interface technology
PHP implements the method of sorting multi-dimensional arrays according to a certain key value
PHP implements third-party instant access to logistics updates
The above is the detailed content of Detailed explanation of 2 methods of merging arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!