Php 乘法和加法2个数组

Php 乘法和加法2个数组,php,arrays,add,multiplication,Php,Arrays,Add,Multiplication,我有两个变量,一个名为$corp\u resp: var_dump($corp_resp) 串(3)“0.3”串(4)“0.35”串(3)“0.4” 其他:$corp\u resp\u模板 var_dump($corp_res_template)´ 串(3)“0.4”串(3)“0.6”串(3)“0.8” 我想对数组进行加法和乘法: $total = (0.3*0.4)+(0.35*0.6) +(0.4*0.8) => 0,12+0.21+0,32 $total = 0.65 最好

我有两个变量,一个名为
$corp\u res
p:

var_dump($corp_resp)
串(3)“0.3”串(4)“0.35”串(3)“0.4”

其他:
$corp\u resp\u模板

var_dump($corp_res_template)´
串(3)“0.4”串(3)“0.6”串(3)“0.8”

我想对数组进行加法和乘法:

$total = (0.3*0.4)+(0.35*0.6) +(0.4*0.8) => 0,12+0.21+0,32 

$total = 0.65

最好的方法是什么?

如果两个阵列的长度相同,您可以运行以下操作:

array_sum(array_map(
    function($resp, $tpl){ return $resp * $tpl; }, 
$corp_resp, $corp_res_template));

如果数组长度不相等,则较长数组的尾部将计算为(数字*0),因此在合计最终结果时忽略它们

如果两个数组的长度相同,则可以运行如下操作:

array_sum(array_map(
    function($resp, $tpl){ return $resp * $tpl; }, 
$corp_resp, $corp_res_template));

如果数组长度不等,则较长数组的尾部将计算为(数字*0),因此在合计最终结果时忽略它们

$corp_resp = array("0.3", "0.35", "0.4");
$corp_res_template = array("0.4", "0.6", "0.8");

function add_products($a1, $a2) {
    if (!is_array($a1)) {
            throw new Exception("a1 is not an array");
    }
    if (!is_array($a2)) {
            throw new Exception("a2 is not an array");
    }
    if (sizeof($a1) != sizeof($a2)) {
            throw new Exception("Arrays don't have same number of elements");
    } 

    // both params are arrays and have same number of elements!

    $count = sizeof($a1);
    $multiplied = array();
    for($i=0; $i<$count; $i++) {
            // we assume that each element is a string representing a floatval so we need to cast as a float before multiplying
            $multiplied[$i] = floatval($a1[$i]) * floatval($a2[$i]);
    }
    return array_sum($multiplied);
}

$val = add_products($corp_resp, $corp_res_template);

var_dump($val);
$corp_resp=array(“0.3”、“0.35”、“0.4”);
$corp_res_模板=数组(“0.4”、“0.6”、“0.8”);
功能添加产品($a1,$a2){
如果(!是_数组($a1)){
抛出新异常(“a1不是数组”);
}
如果(!是_数组($a2)){
抛出新异常(“a2不是数组”);
}
如果(sizeof($a1)!=sizeof($a2)){
抛出新异常(“数组的元素数不相同”);
} 
//两个参数都是数组,元素数相同!
$count=sizeof($a1);
$multipled=array();

对于($i=0;$i编写一个函数来执行此操作

$corp_resp = array("0.3", "0.35", "0.4");
$corp_res_template = array("0.4", "0.6", "0.8");

function add_products($a1, $a2) {
    if (!is_array($a1)) {
            throw new Exception("a1 is not an array");
    }
    if (!is_array($a2)) {
            throw new Exception("a2 is not an array");
    }
    if (sizeof($a1) != sizeof($a2)) {
            throw new Exception("Arrays don't have same number of elements");
    } 

    // both params are arrays and have same number of elements!

    $count = sizeof($a1);
    $multiplied = array();
    for($i=0; $i<$count; $i++) {
            // we assume that each element is a string representing a floatval so we need to cast as a float before multiplying
            $multiplied[$i] = floatval($a1[$i]) * floatval($a2[$i]);
    }
    return array_sum($multiplied);
}

$val = add_products($corp_resp, $corp_res_template);

var_dump($val);
$corp_resp=array(“0.3”、“0.35”、“0.4”);
$corp_res_模板=数组(“0.4”、“0.6”、“0.8”);
功能添加产品($a1,$a2){
如果(!是_数组($a1)){
抛出新异常(“a1不是数组”);
}
如果(!是_数组($a2)){
抛出新异常(“a2不是数组”);
}
如果(sizeof($a1)!=sizeof($a2)){
抛出新异常(“数组的元素数不相同”);
} 
//两个参数都是数组,元素数相同!
$count=sizeof($a1);
$multipled=array();

对于($i=0;$i)你能告诉我们你到目前为止做了什么吗?$sum=0;对于($i==0$i@ChristopheCosta这是我给你看的伪代码。看看一些基本的for循环语法,看看如何做,而不是让别人为你写代码。顺便说一句,这段代码中唯一缺少的是
$I++
部分,在
count()之后
function(抱歉,我认为跳过它已经足够明显了)。您好,例如,$sum=0;for($I=0;$I$I++
部分,在
count()
函数之后(对不起,我认为跳过它已经足够明显了).Hello AL.G.,$sum=0;for($i=0;$i