Home > Backend Development > PHP Tutorial > How Can I Efficiently Merge Two 2D Arrays in PHP Based on a Shared Column Value?

How Can I Efficiently Merge Two 2D Arrays in PHP Based on a Shared Column Value?

DDD
Release: 2024-12-11 08:29:11
Original
412 people have browsed it

How Can I Efficiently Merge Two 2D Arrays in PHP Based on a Shared Column Value?

Merging 2D Arrays by Shared Column Value

Combining two multidimensional arrays with shared values can be a common programming task. While traditional iterative approaches using nested loops and conditional checks are reliable, they can often lead to verbose and potentially inefficient code. This article explores elegant solutions using native PHP array functions.

Consider the following two arrays:

$array1 = [
    ['rank' => '579', 'id' => '1'],
    ['rank' => '251', 'id' => '2'],
];

$array2 = [
    ['size' => 'S', 'status' => 'A', 'id' => '1'],
    ['size' => 'L', 'status' => 'A', 'id' => '2'],
];
Copy after login

The goal is to merge these arrays based on their shared 'id' values, producing the following result:

[
    ['size' => 'S', 'status' => 'A', 'id' => '1', 'rank' => '579'],
    ['size' => 'L', 'status' => 'A', 'id' => '2', 'rank' => '251'],
]
Copy after login

Solution 1: array_merge_recursive

PHP's array_merge_recursive() function enables the merging of arrays, recursively combining elements with matching keys.

$mergedArray = array_merge_recursive($array1, $array2);
Copy after login

This solution is straightforward and produces the desired result without the need for additional loops or conditions.

Solution 2: Custom Function

Alternatively, a custom function can be created to perform the merge in a potentially more efficient manner:

function my_array_merge(array &$array1, array &$array2) {
    $result = [];
    foreach ($array1 as $key => &$value) {
        $result[$key] = array_merge($value, $array2[$key]);
    }
    return $result;
}

$mergedArray = my_array_merge($array1, $array2);
Copy after login

This function iterates through the first array, merging each element with its corresponding element from the second array based on the matching keys.

Both solutions provide effective ways to merge 2D arrays based on shared column values. The specific approach chosen may depend on factors such as performance requirements or code readability preferences.

The above is the detailed content of How Can I Efficiently Merge Two 2D Arrays in PHP Based on a Shared Column Value?. For more information, please follow other related articles on the PHP Chinese website!

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