从多维数组求和/合并值

时间:2015-05-28 15:42:04

标签: php arrays multidimensional-array

如何组合来自多维数组的值?

Array1

Array
(
    [Amie] => Array
        (
            [0] => Amie
            [1] => 12
            [2] => 5
            [3] => 3.3%
            [4] => 0.0
            [5] => 83.2
            [6] => 4.9
            [7] => 772.4
            [8] => 64.4
            [9] => 0.0
            [10] => 0.0
            [11] => 0.0
            [12] => 0.0
            [13] => 0.0
            [14] => 0.0%
            [15] => 7
            [16] => 5
            [17] => 58.3%
            [18] => 41.7%
        )
)

ARRAY2

Array
(
    [Amie] => Array
        (
            [0] => Amie
            [1] => 16
            [3] => 13.1%
            [4] => 0.0
            [5] => 129.0
            [6] => 9.2
            [7] => 1692.4
            [8] => 120.2
            [9] => 0
            [10] => 0.0
            [11] => 0
            [12] => 0.0
            [13] => 0.0
            [14] => 0.0%
            [15] => 8
            [16] => 6
            [17] => 57.1%
            [18] => 42.9%
      )
)

最终数组应该组合所有值,因此最终结果将如下所示:

[Amie] => Array
            (
                [0] => Amie
                [1] => 28
                [2] => 5
                [3] => 16.4%
                [4] => 0.0
                [5] => 212.
                [6] => etc.

尝试将一个数组保留为原始数组,然后合并两个数组并查找需要更新的值。我也试过从头开始创建这个数组,如:

$finalArray = array_map(function () {
                                return array_sum(func_get_args());
                            },  array_splice($processedReport1, 1), 
                                array_splice($processedReport2, 1));

或使用foreach和嵌套的foreach循环,所有方法都给我一些东西或一些奇怪的东西:O

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您必须手动遍历每个条目,检查它是否有百分号,必要时修剪然后对数组求和。像这样:

function sum_arrays (array $arr1, array $arr2)
{
    $sum = array();
    foreach ($arr1 as $key => $val)
    {
        // First key is string, skip
        if ($key == 0) continue;

        // Check for percentage sign
        $p = false;
        if (strpos($val, '%'))
        {
            $p = true;
        }

        $sum[$key] = trim($val, '%');
        if (isset($arr2[$key]))
        {
            $sum[$key] += trim($arr2[$key], '%');
        }

        if ($p)
        {
            $sum[$key] .= '%';
        }
    }
    return $sum;
}

$sum = sum_arrays($array1, $array2);

NB:我没有测试过此解决方案。

答案 1 :(得分:0)

在下文中,我合并了数组的键,并检查了两个数组的union数组中是否存在每个键并进行了适当的操作;即:值最终转换为float s,在字符串百分比的情况下,首先提取数字部分。

请注意,隐式array_unique也会在array_merge的实现中应用,从而获得联合。

<?php
function combine_arrays_customized($a1, $a2) {
    $keys = array_merge(array_keys($a1), array_keys($a2));
    $combinedArr = array(0=>$a1[0]);
    foreach($keys as $key) {
        if($key == 0)
            continue;

        $val1 = $val2 = 0;
        $isPercentage = false;
        if(isset($a1[$key])) {
            $v = $a1[$key];
            if(is_string($v) && preg_match('~\d+\.?\d*%$~', $v)) {
                $isPercentage = true;
                $val1 = (float)substr($v, 0, -1);
            }
            else $val1 = (float)$v;
        }
        if(isset($a2[$key])) {
            $v = $a2[$key];
            if(is_string($v) && preg_match('~\d+\.?\d*%$~', $v)) {
                $isPercentage = true;
                $val2 = (float)substr($v, 0, -1);
            }
            else $val2 = (float)$v;
        }
        if($isPercentage)
            $combinedArr[$key] = ($val1 + $val2) . '%';
        else $combinedArr[$key] = ($val1 + $val2);
    }
    return $combinedArr;
}

// test
$a1 = array(0 => 'Amie', 1 => '12', 2 => '5', 3 => '3.3%', 4 => '0.0', 5 => '83.2', 6 => '4.9', 7 => '772.4', 8 => '64.4', 9 => '0.0', 10 => '0.0', 11 => '0.0', 12 => '0.0', 13 => '0.0', 14 => '0.0%', 15 => '7', 16 => '5', 17 => '58.3%', 18 => '41.7%');
$a2 = array( 0 => 'Amie', 1 => '16', 3 => '13.1%', 4 => '0.0', 5 => '129.0', 6 => '9.2', 7 => '1692.4', 8 => '120.2', 9 => '0', 10 => '0.0', 11 => '0', 12 => '0.0', 13 => '0.0', 14 => '0.0%', 15 => '8', 16 => '6', 17 => '57.1%', 18 => '42.9%' );

echo "<pre>";
var_dump(combine_arrays_customized($a1, $a2));
echo "</pre>";
?>