How to merge arrays in php without changing the key

青灯夜游
Release: 2023-03-11 15:24:02
Original
2716 people have browsed it

Method: 1. Use the " " operator, the syntax is "array 1 array 2"; 2. Use the array_merge_recursive() function. When two or more array elements have the same key name, no Instead of key name overwriting, multiple values ​​with the same key name are recursively formed into an array.

How to merge arrays in php without changing the key

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

Generally, array_merge is used to merge two arrays in PHP ()

For example:

$data1 = ['111' => 'aaa', '222' => 'bbb', '333' => 'ccc'];
$data2 = ['444' => 'ddd', '555' => 'eee', '666' => 'fff'];
$data = array_merge($data1, $data2);
var_dump($data);
Copy after login

Get the result:

array(6) {
  [0]=>
  string(3) "aaa"
  [1]=>
  string(3) "bbb"
  [2]=>
  string(3) "ccc"
  [3]=>
  string(3) "ddd"
  [4]=>
  string(3) "eee"
  [5]=>
  string(3) "fff"
}
Copy after login

You can see that using array_merge() will reset the key value. If the key value is useful to us, we don’t want to reset it. , you canuse " "to merge arrays.

$data1 = ['111' => 'aaa', '222' => 'bbb', '333' => 'ccc'];
$data2 = ['444' => 'ddd', '555' => 'eee', '666' => 'fff'];
$data = $data1 + $data2;
var_dump($data);
Copy after login

Get the result:

array(6) {
  [111]=>
  string(3) "aaa"
  [222]=>
  string(3) "bbb"
  [333]=>
  string(3) "ccc"
  [444]=>
  string(3) "ddd"
  [555]=>
  string(3) "eee"
  [666]=>
  string(3) "fff"
}
Copy after login

You can alsoUse the array_merge_recursive function to merge the cells of one or more arrays, and the values ​​in one array are appended to the previous one behind the array. Returns the resulting array.

This function is an upgraded version of array_merge. It adds the following functions on the basis of array_merge: If the key value is the same, it is the same as array_merge when it is a number, and re-indexed; if it is a string, it will no longer overwrite the previous one. Instead, it is appended to the back in a recursive manner; for example:

 /* array_merge_recursive */
$jiaArr = array(
	'name1'=>'xiaoli',
	'name2'=>'xiaohua',
	'name3'=>'xiaoming',
	'1'=>'teacher',
);
 
$jiaBrr = array(
	'name1'=>'xiaolis',
	'name5'=>'xiaohuas',
	'name6'=>'xiaomings',
	'1'=>'teachers',
);
$jiaAll = array_merge_recursive($jiaArr,$jiaBrr);
var_dump($jiaAll);
 
/*浏览器output:
array(7) {
  ["name1"]=>
  array(2) {
    [0]=>
    string(6) "xiaoli"
    [1]=>
    string(7) "xiaolis"
  }
  ["name2"]=>
  string(7) "xiaohua"
  ["name3"]=>
  string(8) "xiaoming"
  [0]=>
  string(7) "teacher"
  ["name5"]=>
  string(8) "xiaohuas"
  ["name6"]=>
  string(9) "xiaomings"
  [1]=>
  string(8) "teachers"
}
*/
Copy after login

name1 is present in both arrays, but is not overwritten but appended recursively, and the 1 array key values ​​are still reordered;

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to merge arrays in php without changing the key. 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