对多个数组的列值求和

Sum column values from multiple arrays

我有一个动态名称的数组。我的数组可能超过 3 个,取决于数组变量应该是唯一的

$loopThrice = 3;
$getSum = 0;
$total = array();

$array0 = array(5, 10, 15, 20, 25, 30);
$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(2, 6, 8, 10, 12, 14);

for($a=0; $a < $loopThrice; $a++){ // loop 3x to get unique array name
    foreach (${'array'.$a} as $key => $value) { // $array0, $array1, $array2,  
        //Right here is my problem, I'm not sure if this the correct way to get the sum of $array0,1,2
        
        //get the sum of array0,1,2 -- newarray(8, 18, 26, 34, 42, 50)
        $getSum += 
            //store newarray
            array_push($total, $getSum);     
    }
}

我需要得到这样的输出:

Array ( 
    [0] => 8
    [1] => 18 
    [2] => 26 
    [3] => 34 
    [4] => 43 
    [5] => 50 
)

这应该适合你:

$array0 = array(5, 10, 15, 20, 25, 30);
$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(2, 6, 8, 10, 12, 14);

$var_prefix = 'array';
$arr_count = 0;
$max_fields = 0;

while(isset(${$var_prefix.$arr_count})) {
    $data[] = ${$var_prefix.$arr_count};
    if(count(${$var_prefix.$arr_count})>$max_fields) {
        $max_fields = count(${$var_prefix.$arr_count});
    };

    $arr_count++;
}

for($i=0; $i<$max_fields; $i++) {
    $result[$i] = array_sum(array_column($data, $i));
}

echo '<pre>';
print_r($result);
echo '</pre>';
die();

好吧,多阵列是可行的方法,但您仍然可以这样做:

 $loopThrice = 3;
 $getSum = 0;
 $total = array();

 $array0 = array(5, 10, 15, 20, 25, 30);
 $array1 = array(1, 2, 3, 4, 5, 6);
 $array2 = array(2, 6, 8, 10, 12, 14);
 // find which arrray has the most values
 for($a=0; $a < $loopThrice; $a++){
     $max_index = (count(${'array'.$a}) > $max_index ? count(${'array'.$a}) : $max_index);
 }

 for($i=0; $i < $max_index; $i++){
      for($a=0; $a < $loopThrice; $a++){
           $total[$i] += ${'array'.$a}[$i];
     }
 }

 print_r($total);
 // prints Array ( [0] => 8 [1] => 18 [2] => 26 [3] => 34 [4] => 42 [5] => 50 ) 

为什么不使用多维数组?

$array = array(); // hungarian notation
$array[] = array(5, 10, 15, 20, 25, 30);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10, 12, 14);

在这种情况下,您将拥有一个数组数组:

Array
(
[0] => Array
    (
        [0] => 5
        [1] => 10
        [2] => 15
        [3] => 20
        [4] => 25
        [5] => 30
    )

[1] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
    )

[2] => Array
    (
        [0] => 2
        [1] => 6
        [2] => 8
        [3] => 10
        [4] => 12
        [5] => 14
    )
)

您可以使用嵌套的 for 循环:

$sumArray = array();
$arrayCount = count($array);
$elementCount = 0;

foreach($array as $subarray)
{
    $count = count($subarray);
    $elementCount = $elementCount < $count ? $count : $elementCount;
}

for($i = 0; $i < $elementCount; $i++)
{
    $sumArray[$i] = 0;

    for($j = 0; $j < $arrayCount; $j++)
    {
        $sumArray[$i] += $array[$j][$i];
    }
}

print_r($sumArray);

输出为

Array
(
    [0] => 8
    [1] => 18
    [2] => 26
    [3] => 34
    [4] => 42
    [5] => 50
)

现在,如果你有一个不成比例的子数组(即每个子数组中元素的数量不同),你仍然会得到某种结果,因为缺失的元素将被假定为 0 .因此,输入:

$array = array(); // hungarian notation
$array[] = array(5, 10, 15, 20, 25);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10);

你仍然会得到结果:

Array
(
    [0] => 8
    [1] => 18
    [2] => 26
    [3] => 34
    [4] => 30
    [5] => 6
)

我不知道为什么你有单独的数组变量,但这个过程与你将包含这些数组的单个数组声明为多维数组中的行一样。

一旦你有了一个多维数组,展开运算符 (...) 就会将数据列输入到 array_map() 的自定义函数体中——其中 array_sum() 可以是呼吁。

此任务正在对转置数据进行有效求和。

代码:(Demo)

var_export(
    array_map(fn() => array_sum(func_get_args()), $array0, $array1, $array2)
);

或者:

var_export(
    array_map(fn(...$cols) => array_sum($cols), $array0, $array1, $array2)
);

输出(来自任一方法):

array (
  0 => 8,
  1 => 18,
  2 => 26,
  3 => 34,
  4 => 42,
  5 => 50,
)