0

我有一个动态名称的数组。我的数组可能超过 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 
)
4

4 回答 4

3

你为什么不使用多维数组?

$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
)
于 2016-09-14T14:32:28.483 回答
2

那么多数组是要走的路,但你仍然可以这样做:

 $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 ) 
于 2016-09-14T14:29:01.887 回答
1

这应该适合你:

$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();
于 2016-09-14T14:20:29.167 回答
0

我不知道为什么你有单独的数组变量,但是如果你只是声明一个包含这些数组的单个数组作为多维数组中的行,这个过程就会变得更清晰。一旦你有了一个多维数组,展开运算符 ( ...) 会将行输入array_map()null回调将参与转置行为。

  1. 在转置输入数组时合并。( array_map(null, ...$arrays))
  2. 调用array_sum()每一行(列在步骤 1 中转换为行)。

代码:(演示

var_export(
    array_map('array_sum', array_map(null, $array0, $array1, $array2))
);

输出:

array (
  0 => 8,
  1 => 18,
  2 => 26,
  3 => 34,
  4 => 42,
  5 => 50,
)
于 2021-08-30T01:25:21.960 回答