English 中文(简体)
1. 阵列中的自动数量
原标题:Automate sum in array

I will try to explain the problem that I have with this code.

This script works well to up to three persons ($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

但是,如果我只有4人(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

因此,问题在于实现这些变化的自动化。

此处为矩阵表 al:

“Enterography

我所希望的唯一事情是使我的准则充满活力地发挥众多人员的作用。

A   |  B |  C |  D
Person1
Person2
Person3
Person4
...

我的情况可能有所不同的是人数。

更多信息here

最佳回答
$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);
问题回答
$subtotal = 0;
for ($i = 0; $i < $numRows; $i++) {
   $subtotal += ${ contaH .$z}[$i] * $final[$i];
}
$total[] = $subtotal;

https://pear.php.net/ Package/Math_Matrix"rel=“nofollow” 图书馆将帮助你做各种算术。

然而,以下法典使你的解决办法自动化:

function mat_mult($matrix, $vector) {
    $result = array();
    $matrixWidth = count($matrix[0]);
    for ($z = 0; $z < $matrixWidth; $z++) {
        $value = 0;
        for ($y = 0; $y < $matrixWidth; $y++) {
            $value += $matrix[$z][$y]*$vector[$y];
        }
        $result[] = $value;
    }
    return $result;
}

$matrix = array(
    array(1, 1/3.0, 2, 4),
    array(3, 1, 5, 3),
    array(1/2.0, 1/5.0, 1, 1/3.0),
    array(1/4.0, 1/3.0, 3, 1)
);
$vector = array(0.26, 0.50, 0.09, 0.16);

$v2 = mat_mult($matrix, $vector);

print_r($v2);

此外,为了将其进一步纳入现有的矩阵结构:

$matrix = array();
for ($z = 0; $z < $numRows; $z++) {
    $matrix[] = ${ contaH .$z};
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签