2 ways to merge arrays in PHP

不言
Release: 2023-03-24 08:24:02
Original
3228 people have browsed it

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 the array_merge() function, but recently I When I changed jobs, I encountered an interview question about merging arrays. What I thought at the time 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);
Copy after login

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
Copy after login

The requirement is to get the same effect without using array_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);
Copy after login

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
Copy after login

From The result can be seen that array_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 with array_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);
Copy after login

Output result:

array (size=4)
 'color' => string 'red' (length=3)
 0 => int 5
 1 => int 6
 'type' => string 'fruit' (length=5)
Copy after login

It can be seen from this result that using the ' ' sign to merge arrays covers the front and back, and array_merge() On the contrary, and it is more ruthless than array_merge(). If the contents of the array appear in index form, they will be overwritten if the key values ​​are the same after merging!

Related recommendations:

PHP merges two one-dimensional arrays

php merge array function array_merge()

The above is the detailed content of 2 ways to merge arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template