Home > Article > Backend Development > How to merge one or more arrays in PHP
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"]
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"]
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]
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"]
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], ], ]
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], ], ]
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!