Home >Backend Development >PHP Problem >PHP implements majority group merging

PHP implements majority group merging

WBOY
WBOYOriginal
2023-05-07 13:30:111208browse

In PHP development, we often encounter situations where multiple arrays need to be merged. The merged array needs to contain all the elements in the original array and retain the relationship of each key-value pair. This article will introduce how to achieve majority group merging through PHP.

1. array_merge function

PHP’s built-in array_merge function can merge two or more arrays. It will merge the elements of all arrays into a new array if they have the same key name. , then the later value will overwrite the previous value.

The sample code is as follows:

$arr1 = array('a', 'b', 'c');
$arr2 = array('d', 'e', 'f');
$arr3 = array('g', 'h', 'i');

$newArr = array_merge($arr1, $arr2, $arr3);

print_r($newArr);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
)

2. Custom function

If the same value exists in multiple arrays to be merged Key name, the array_merge function will overwrite the previous value, which does not meet the requirements. In order to solve this problem, you can customize a function to achieve majority combination merging.

The logic of the custom function is: traverse all the key-value pairs of the array, save the value of the same key name to a new array, if the same key name exists, save the value to a In the array indexed by the key name, if it does not exist, the value is saved to a new array element.

The sample code is as follows:

function multiArrayMerge(...$arrays)
{
    $result = array();
    foreach ($arrays as $array) {
        foreach ($array as $key => $value) {
            if (is_int($key)) {
                $result[] = $value;
            } else {
                if (array_key_exists($key, $result) && is_array($value)) {
                    $result[$key] = multiArrayMerge($result[$key], $value);
                } else {
                    $result[$key] = $value;
                }
            }
        }
    }
    return $result;
}

Example of calling multiple array merge functions:

$arr1 = array(
    'a' => 1,
    'b' => array(
        'c' => 2,
        'd' => array(
            'e' => 3
        )
    )
);

$arr2 = array(
    'a' => 4,
    'b' => array(
        'c' => 5,
        'd' => array(
            'f' => 6
        )
    )
);

$arr3 = array(
    'g' => 7,
    'h' => array(
        'i' => 8
    )
);

$result = multiArrayMerge($arr1, $arr2, $arr3);

print_r($result);

The output result is:

Array
(
    [a] => 4
    [b] => Array
        (
            [c] => 5
            [d] => Array
                (
                    [e] => 3
                    [f] => 6
                )

        )

    [g] => 7
    [h] => Array
        (
            [i] => 8
        )

)

3. Precautions

When using custom functions to achieve majority group merging, you need to pay attention to some issues:

1. If there are the same key names and the values ​​​​of different arrays are merged, data conflicts may occur. You need to consider the specific Business logic is processed.

2. In order to prevent infinite recursion of the function, it is necessary to judge the value in the array. If it is an array, perform a recursive operation, otherwise the value will be saved directly to the result array.

3. The three dots (...) before the parameters indicate a variable-length parameter list, which can accept any number of parameters. The parameter list will be automatically converted into an array for use.

4. Summary

PHP provides the array_merge function to merge arrays, but when the key-value pair relationship needs to be preserved, there will be a problem of overwriting values ​​with the same key name. This article introduces a method to implement a custom function, which is suitable for merging multiple arrays and needing to preserve the key-value pair relationship. When using it, you need to pay attention to the problem of data conflicts with the same key names.

The above is the detailed content of PHP implements majority group merging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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