How to merge one or more arrays in PHP

王林
Release: 2024-03-19 10:44:02
forward
1140 people have browsed it

php editor Banana will introduce you in detail how to merge one or more arrays. In PHP, you can use the array_merge() function to merge multiple arrays into a new array, or you can use the plus operator () to merge arrays. In addition, the array_merge_recursive() function can merge multi-dimensional arrays. For associative arrays, you can use the " " operator to combine them. In actual development, appropriate methods are selected for array merging based on specific needs to make the program more efficient and flexible.

PHP merge array

php Provides multiple methods to merge one or more arrays:

array_merge() function

array_merge() function is the simplest way to merge arrays. It merges multiple arrays into a new array.

$arr1 = [1, 2, 3];
$arr2 = ["a", "b", "c"];

$mergedArr = array_merge($arr1, $arr2);

print_r($mergedArr); // Output [1, 2, 3, "a", "b", "c"]
Copy after login

Operator

The operator can also merge arrays, but is limited to two arrays.

$arr1 = [1, 2, 3];
$arr2 = ["a", "b", "c"];

$mergedArr = $arr1 $arr2;

print_r($mergedArr); // Output [1, 2, 3, "a", "b", "c"]
Copy after login

array_combine() function

array_combine() The function creates a new array with elements of one array as keys and elements of the other array as values.

$keys = ["id", "name", "age"];
$values ​​= [1, "John", 30];

$mergedArr = array_combine($keys, $values);

print_r($mergedArr); // Output ["id" => 1, "name" => "John", "age" => 30]
Copy after login

array_map() function

array_map() The function allows you to apply one or more functions to each array element and then returns a new array. This can be used to merge arrays, such as adding corresponding elements of two arrays.

$arr1 = [1, 2, 3];
$arr2 = ["a", "b", "c"];

$mergedArr = array_map(function($a, $b) { return $a . $b; }, $arr1, $arr2);

print_r($mergedArr); // Output ["1a", "2b", "3c"]
Copy after login

Nested Array

If you need to merge nested arrays, you can use the recursive function or the array_merge_recursive() function.

array_merge_recursive() function

array_merge_recursive() The function recursively merges arrays and merges all child elements in nested arrays.

$arr1 = [
"name" => "John",
"age" => 30,
"children" => [
["name" => "Alice", "age" => 5],
["name" => "Bob", "age" => 8],
],
];

$arr2 = [
"name" => "Jane",
"age" => 35,
"children" => [
["name" => "Carol", "age" => 10],
],
];

$mergedArr = array_merge_recursive($arr1, $arr2);

print_r($mergedArr); // Output [
"name" => "Jane",
"age" => 35,
"children" => [
["name" => "Alice", "age" => 5],
["name" => "Bob", "age" => 8],
["name" => "Carol", "age" => 10],
],
]
Copy after login

recursive function

You can also use recursive functions to merge nested arrays.

function mergeNestedArrays($arr1, $arr2) {
foreach ($arr2 as $key => $value) {
if (is_array($value) && isset($arr1[$key]) && is_array($arr1[$key])) {
$arr1[$key] = mergeNestedArrays($arr1[$key], $value);
} else {
$arr1[$key] = $value;
}
}

return $arr1;
}

$arr1 = [
"name" => "John",
"age" => 30,
"children" => [
["name" => "Alice", "age" => 5],
["name" => "Bob", "age" => 8],
],
];

$arr2 = [
"name" => "Jane",
"age" => 35,
"children" => [
["name" => "Carol", "age" => 10],
],
];

$mergedArr = mergeNestedArrays($arr1, $arr2);

print_r($mergedArr); // 输出 [
"name" => "Jane",
"age" => 35,
"children" => [
["name" => "Alice", "age" => 5],
["name" => "Bob", "age" => 8],
["name" => "Carol", "age" => 10],
],
]
Copy after login

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

source:lsjlt.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!