Multiply matching keys in two arrays

Oh ok. I do have a question though: Say I have two arrays where the first array contains the number of “tutoring sessions” and the second array contains the “lesson duration”:


Array ([0] => 2  [1] => 3 ) 
Array ([0] => 45  [1] => 90 )

and what I want to do is multiply the matching keys between the two. For example, key of [0] is in both arrays and i want to multiply 2 * 45 and 3 * 90.

In addition do that I want to take those results and add them together to get total minutes (in this example, it is 90 + 270 = 360). From there I would take the result and say like “Total lesson minutes: 360”

Thanks ScallioXTX! Your solution helped me out and now I know something to use in the future that I was stumped with before. This forum never disappoints with the quick and concise help.


<?php
function multiply($a, $b){
  return $a * $b;
}

function stuff($arrayOne, $arrayTwo){
  $computed = array();
  for($index = 0; $index < count($arrayOne); $index++){
    array_push($computed, multiply($arrayOne[$index], $arrayTwo[$index]));
  }
  return array_sum($computed);
}

echo stuff(
  array(100, 5),
  array(5, 10)
); #550

?>

Off Topic:

Dammit ScallioXTX!


$sessions = array(2, 3);
$duration = array(45, 90);
$total = 0;
foreach($sessions as $i => $num) {
  if (isset($duration[$i]))
    $total += $num * $duration[$i];
}
echo 'Total lesson minutes: ', $total;

:slight_smile: