Home > Backend Development > PHP Tutorial > How to Merge Multiple Associative Arrays and Sum Values of Shared Keys in PHP?

How to Merge Multiple Associative Arrays and Sum Values of Shared Keys in PHP?

Linda Hamilton
Release: 2024-11-03 16:08:30
Original
944 people have browsed it

How to Merge Multiple Associative Arrays and Sum Values of Shared Keys in PHP?

How to Merge Multiple Associative Arrays and Sum Values of Shared Keys

The Challenge

As you may have encountered, the built-in array_merge() function in PHP overwrites the values of shared keys when combining multiple arrays. This can be problematic when you need to merge arrays and sum the values of shared keys instead.

A Solution

To address this challenge, we can create a custom function or leverage array operations to achieve our desired behavior. Let's explore a few possible approaches:

Using a Custom Function

<code class="php">function array_sum_identical_keys() {
    $arrays = func_get_args();

    $keys = array_keys(array_reduce($arrays, function ($keys, $arr) { return $keys + $arr; }, array()));
    $sums = array();

    foreach ($keys as $key) {
        $sums[$key] = array_reduce($arrays, function ($sum, $arr) use ($key) { return $sum + @$arr[$key]; });
    }

    return $sums;
}</code>
Copy after login

This function accepts an arbitrary number of arrays as arguments and returns an associative array with the combined keys and summed values.

Using Error Suppression

<code class="php">$sums = array();
foreach (array_keys($a1 + $a2) as $key) {
    $sums[$key] = @($a1[$key] + $a2[$key]);
}</code>
Copy after login

This technique leverages the error suppression operator (@) to check for the existence of keys in both arrays before performing the addition.

Using Array Mappings

<code class="php">$keys = array_fill_keys(array_keys($a1 + $a2), 0);
$sums = array_map(function ($a1, $a2) { return $a1 + $a2; }, array_merge($keys, $a1), array_merge($keys, $a2));</code>
Copy after login

This approach creates a mapping using array_fill_keys() and then uses array_map() to add the values associated with the shared keys.

Combining Solutions

<code class="php">$sums = array_fill_keys(array_keys($a1 + $a2), 0);
array_walk($sums, function (&amp;$value, $key, $arrs) { $value = @($arrs[0][$key] + $arrs[1][$key]); }, array($a1, $a2));</code>
Copy after login

This combination combines the error suppression technique with array_walk() to iterate over the keys and perform the addition.

Conclusion

Depending on your specific requirements, you can choose the most suitable method to merge multiple arrays and sum the values of shared keys. Whether it's using a custom function, error suppression, array mappings, or a combination thereof, these solutions provide a concise and effective way to achieve the desired result.

The above is the detailed content of How to Merge Multiple Associative Arrays and Sum Values of Shared Keys in PHP?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template