合并/求和多维数组php [英] merge/sum multi dimentional array php

查看:136
本文介绍了合并/求和多维数组php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并/求和两个可以包含整数或更多数组的数组(它们本身包含整数).

I'm trying to merge/sums 2 arrays that can contain integers or more arrays (themselves containing integer).

当值是整数时,我需要将它们加到最终数组中. 当值是数组时,我需要遍历值并将它们求和到最终数组中.

When the values are integers, I need to sum them in the final array. When the values are arrays, I need to loop through the values and sum them in the final array.

如果值或子数组仅存在于基本数组的1个中,则需要将其添加到最终数组的子数组中. (这是我不能做的.)

If a value or a sub-array exists only in 1 of the base array, it needs to be added in the sub-array of the final array. (This is what I can't do)..)

我的数组是这样的:

 ARRAY 1
[1466859600] => Array
    (
        [TOTAL] => 27217
        [AAA] => Array
            (
                [FD_CDP] => 1746
                [LO_SC_MIC] => 4654
                [FD_ATS] => 893
                [CDP] => 40
                [SUPERVISION] => 9
                [CONTROL] => 4
                [ATS] => 4
                [EVT_ACK] => 3
            )

        [BBB] => Array
            (
                [FD_CDP] => 1376
                [LO_SC_MIC] => 4606
                [FD_ATS] => 826
                [FD_ATSS] => 451
                [LO_SFRC] => 4
                [FD_S2] => 259
                [2_LOSC] => 2
            )

        [CCC] => Array
            (
                [FD_CDP] => 1333
                [LO_SC_MIC] => 4725
                [FD_ATS] => 856
                [CONTROL] => 4
                [ATS] => 2
                [EVT_ACK] => 5
            )

 ARRAY 2
     [1466859600] => Array
    (
        [TOTAL] => 95406
        [AAA] => Array
            (
                [FD_ATSS] => 1719
                [LO_SC_MIC] => 16830
                [CONTROL] => 16
                [NEW] => 7
                [NOEL] => 206
            )

        [BBB] => Array
            (
                [SUPERVISION] => 23
                [CDP] => 158
                [CONTROL] => 40
                [2_LOSC] => 14
                [ATS] => 6
                [EVT_ACK] => 4
            )

        [CCC] => Array
            (
                [EVT_ACK] => 167
                [LO_SFRC] => 248
                [SUPERVISION] => 23
    )

我写了一个像这样的函数:

I wrote a function like this :

    function sumArrayValues($array1, $array2)
        {
            foreach ($array1 as $key => $value) 
            {

                if (is_array($array1[$key]))
                    {
                        echo "it's an array\n I need to reloop\n";
                        sumArrayValues($array1[$key], $array2[$key]);
                    }
                else
                    {
                        echo "FIRST VALUE TO SUM\n";
                        print_r($array1[$key]."\n");
                        echo "SECOND VALUE TO SUM\n";
                        print_r($array2[$key]."\n");
                        $array1[$key] = (int)$array1[$key] +(int)$array2[$key];
                        echo "--------RESULT of SUM array1&2----------\n";
                    }
            }
        return $array1;

        }

但是此函数未考虑2个(可能还有更多)情况:如果子数组的顺序不同,则子数组或值仅存在于第二个数组中.

But this function doesn't take into account 2 (and probably more) cases: if the sub-array are not in the same order, if a sub-array or a value only exist in second array.

一个函数示例将是一个很好的帮助,但是在更基本的层面上,我什至不知道算法可以做到这一点. 有任何想法吗 ?

A example of function would be a good help, but on a more fundamental level, I even can't figure the algorithm to do that. Any ideas ?

推荐答案

您可以获取foreach的所有密钥循环, 实时演示 .

You can get all the keys for the foreach loop, live demo.

注意,您还可以检查任何数组的键是否未定义,然后保存键的已定义值.

Note, you also can check if a key of any array is undefined, then save the defined value for the key.

function sumArrayValues($array1, $array2)
{
    $keys = array_keys($array1 + $array2);
    foreach ($keys as $key) 
    {
        if (is_array($array1[$key]) || is_array($array2[$key]))
            $array1[$key] = sumArrayValues($array1[$key], $array2[$key]);
        else
            @$array1[$key] = (int)$array1[$key] +(int)$array2[$key];
    }
    return $array1;
}

这篇关于合并/求和多维数组php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆