Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何总结PHP中的加权数组?_Php_Arrays_Interpolation - Fatal编程技术网

如何总结PHP中的加权数组?

如何总结PHP中的加权数组?,php,arrays,interpolation,Php,Arrays,Interpolation,如何将多维数组的值与权重相乘,并将结果汇总到PHP或一般情况下的新数组中 无聊的方式是这样的: $weights = array(0.25, 0.4, 0.2, 0.15); $values = array ( array(5,10,15), array(20,25,30), array(35,40,45), array(50,55,60)

如何将多维数组的值与权重相乘,并将结果汇总到PHP或一般情况下的新数组中

无聊的方式是这样的:

$weights = array(0.25, 0.4, 0.2, 0.15);
$values  = array
           ( 
             array(5,10,15), 
             array(20,25,30), 
             array(35,40,45), 
             array(50,55,60)
           );
$result  = array();

for($i = 0; $i < count($values[0]); ++$i) {
  $result[$i] = 0;
  foreach($weights as $index => $thisWeight)
    $result[$i] += $thisWeight * $values[$index][$i];
}
$weights=数组(0.25,0.4,0.2,0.15);
$values=数组
( 
阵列(5,10,15),
阵列(20,25,30),
阵列(35,40,45),
阵列(50,55,60)
);
$result=array();
对于($i=0;$i$thisweet时的权重)
$result[$i]+=$thisweaght*$values[$index][$i];
}
有更优雅的解决方案吗?

foreach($values as $index => $ary )
  $result[$index] = array_sum($ary) * $weights[$index];


起初我也误解了这个问题

我想,有了这种数据表示,任何其他选择都不如你所拥有的那么清晰

如果我们可以把它换成其他的东西,例如,如果我们把矩阵换成另一种方式,那么很容易得到一种更简洁、可能更优雅的方式

<?php

$weights = array(0.2,0.3,0.4,0.5);
$values = array(array(1,2,0.5), array(1,1,1), array(1,1,1), array(1,1,1));
$result = array();

for($i = 0; $i < count($values[0]); ++$i) {
        $result[$i] = 0;
        foreach($weights as $index => $thisWeight) {
           $result[$i] += $thisWeight * $values[$index][$i];
        }
}
print_r($result);


$result = call_user_func_array("array_map",array_merge(array("weightedSum"),$values));

function weightedSum() {
    global $weights;
    $args = func_get_args();
    return array_sum(array_map("weight",$weights,$args));
}

function weight($w,$a) {
    return $w*$a;
}

print_r($result);

?>

起初我也误解了这个问题

我想,有了这种数据表示,任何其他选择都不如你所拥有的那么清晰

如果我们可以把它换成其他的东西,例如,如果我们把矩阵换成另一种方式,那么很容易得到一种更简洁、可能更优雅的方式

<?php

$weights = array(0.2,0.3,0.4,0.5);
$values = array(array(1,2,0.5), array(1,1,1), array(1,1,1), array(1,1,1));
$result = array();

for($i = 0; $i < count($values[0]); ++$i) {
        $result[$i] = 0;
        foreach($weights as $index => $thisWeight) {
           $result[$i] += $thisWeight * $values[$index][$i];
        }
}
print_r($result);


$result = call_user_func_array("array_map",array_merge(array("weightedSum"),$values));

function weightedSum() {
    global $weights;
    $args = func_get_args();
    return array_sum(array_map("weight",$weights,$args));
}

function weight($w,$a) {
    return $w*$a;
}

print_r($result);

?>


当然取决于你所说的优雅

function weigh(&$vals, $key, $weights) {
    $sum = 0;
    foreach($vals as $v)
        $sum += $v*$weights[$key];
    $vals = $sum;
}

$result = $values;
array_walk($result, "weigh", $weights);
编辑:很抱歉没有更好地阅读您的示例。
我将结果作为值的副本,因为数组通过引用工作。

当然取决于您所说的优雅

function weigh(&$vals, $key, $weights) {
    $sum = 0;
    foreach($vals as $v)
        $sum += $v*$weights[$key];
    $vals = $sum;
}

$result = $values;
array_walk($result, "weigh", $weights);
编辑:很抱歉没有更好地阅读您的示例。
我将结果作为值的副本,因为数组通过引用工作。

我想你误解了我的问题:我希望得到一个加权和的数组,而不是所有和的总和。这意味着$result的维度是1xcount($value[0])。我想你误解了我的问题:我想得到一个加权和的数组,而不是所有和的总和。这意味着$result是维度1xcount($values[0])。嗯,另一个答案去了哪里?嗯,另一个答案去了哪里?这不是他想要的。如果你删除最后一个数组的和。我现在就这么做。这不是他要求的。如果你去掉最后一个数组的和。我现在就去。