1

I want to have that variable $total[$i] giving the result of the following function:

$total[$i] = (array_reduce((array_map(function($x, $y) { return $x * $y; },
                   $corp_resp[$i], $corp_resp_template)),function($carry,$item){return $carry+=$item;},0));

I receive from $corp_resp_templateexample: array(0.4,0.2,0.1) for $corp_resp array(array(sub1(0.2,0.3,0.5) arraysub2(0.2,0.5,0.7)))

$corp_resp_template for that operation is only one. $corp_resp is array with subarrays inside that depends of $carCount in that case $carCount=2 if is 4 will give 4 subarrays, where that values will be interpolated with $corp_resp_template will be only one array the same size of $corp_resp.

Example Operation:

Total 1 =(0.4*0.2+0.2*0.3+0.5*0.1)=0.19 $total[0]

Total 2 =(0.4*0.2+0.2*0.5+0.1*0.7)=0.25 $total[1]

That total values will be inserted in lines of a table.

Thank you.

1
  • Please mention clearly your input and expected output. Commented Apr 18, 2017 at 15:02

1 Answer 1

1

Everything looks quite working:

$corp_resp_template = [0.4,0.2,0.1];
$corp_resp = [[0.2,0.3,0.5],[0.2,0.5,0.7]];

for($i = 0;$i<count($corp_resp);$i++){

    $total[$i] = (array_reduce(array_map(function($x, $y){
      return $x * $y; 
    },$corp_resp[$i], $corp_resp_template),function($carry,$item){return $carry+=$item;},0));
}

print_r($total);

out:

Array
(
   [0] => 0.19
   [1] => 0.25
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.