在数组中自动求和

Dan*_*iel 5 php arrays loops

我将尝试用此代码解释我遇到的问题.

这个脚本适用于最多三个人($ numRows = 3).

$z=0;
$i=0;
$x=0;

do {
    $total[] = (
        ${'contaH'.$z}[$i+0]*$final[$x+0]+
        ${'contaH'.$z}[$i+1]*$final[$x+1]+
        ${'contaH'.$z}[$i+2]*$final[$x+2]
    );
    $z++;
} while ($z<$numRows); //3
Run Code Online (Sandbox Code Playgroud)

但如果我只有四个人($ numRows = 4),我需要这样的东西:

$z=0;
$i=0;
$x=0;

do {
    $total[] = (
        ${'contaH'.$z}[$i+0]*$final[$x+0]+
        ${'contaH'.$z}[$i+1]*$final[$x+1]+
        ${'contaH'.$z}[$i+2]*$final[$x+2]+
        ${'contaH'.$z}[$i+3]*$final[$x+3]
        // if they are 5 persons ($numRows=5), here, should exists another row
    );
    $z++;
} while ($z<$numRows); //4
Run Code Online (Sandbox Code Playgroud)

所以问题是在$ numRows的关系中自动化这些变化.

这是矩阵代数的演示:

在此输入图像描述

我唯一想要的是将我的代码动态地放在一个人的函数中.

A   |  B |  C |  D
Person1
Person2
Person3
Person4
...
Run Code Online (Sandbox Code Playgroud)

在我的情况下可能有什么不同只是人数.

更多信息在这里.

dev*_*ler 2

$z=0;
$i=0;
$x=0;
$numRows = 5;

do{
    $currentSum = 0;
    for($c = 0; $c < $numRows; $c++){
        $currentSum += (${'contaH'.$z}[$i+$c] * $final[$x+$c]);
    }
    $total[] = $currentSum;
    $z++;
}while($z < $numRows);
Run Code Online (Sandbox Code Playgroud)