How to merge multiple arrays into one array according to the specified key name in PHP
In development, we often encounter the need to merge multiple arrays into one array according to the specified key name. This need is very common when working with data, especially when working with database result sets, for example. This article will introduce several common methods to achieve this function and give corresponding code examples.
Method 1: Use loop traversal
The simplest method is to use a loop to traverse all arrays and add the corresponding values to the new array according to the specified key name. The sample code is as follows:
function mergeArrays($key, ...$arrays) { $result = []; foreach ($arrays as $array) { if (isset($array[$key])) { $result[] = $array[$key]; } } return $result; } $array1 = ['id' => 1, 'name' => 'Alice']; $array2 = ['id' => 2, 'name' => 'Bob']; $array3 = ['id' => 3, 'name' => 'Charlie']; $result = mergeArrays('name', $array1, $array2, $array3); print_r($result);
Run the above code, the output result is:
Array ( [0] => Alice [1] => Bob [2] => Charlie )
Method 2: Use array_map function
array_map function can apply a callback function to multiple arrays on and returns a new array. We can use this function to merge multiple arrays. The sample code is as follows:
function mergeArrays($key, ...$arrays) { $callback = function ($array) use ($key) { return $array[$key] ?? null; }; $result = array_map($callback, $arrays); return array_filter($result, function($value) { return $value !== null; }); } $array1 = ['id' => 1, 'name' => 'Alice']; $array2 = ['id' => 2, 'name' => 'Bob']; $array3 = ['id' => 3, 'name' => 'Charlie']; $result = mergeArrays('name', $array1, $array2, $array3); print_r($result);
Run the above code, the output result is:
Array ( [0] => Alice [1] => Bob [2] => Charlie )
Method 3: Use array_reduce function
array_reduce function is used to iterate the elements in the array , process them according to the specified callback function, and return a final result. We can use this function to merge multiple arrays. The sample code is as follows:
function mergeArrays($key, ...$arrays) { $callback = function ($result, $array) use ($key) { if (isset($array[$key])) { $result[] = $array[$key]; } return $result; }; $result = array_reduce($arrays, $callback, []); return $result; } $array1 = ['id' => 1, 'name' => 'Alice']; $array2 = ['id' => 2, 'name' => 'Bob']; $array3 = ['id' => 3, 'name' => 'Charlie']; $result = mergeArrays('name', $array1, $array2, $array3); print_r($result);
Run the above code, the output result is:
Array ( [0] => Alice [1] => Bob [2] => Charlie )
The above are examples of several common methods to merge multiple arrays into one array according to the specified key name. Choosing the appropriate method according to the actual situation can greatly improve the efficiency and readability of the code. Hope this helps!
The above is the detailed content of How to merge multiple arrays into one array according to specified key names in PHP. For more information, please follow other related articles on the PHP Chinese website!