If you are implementing in php then you can use foreach loop instead of for loop shown as below: If you have two vectors with unknown number of values then use this.
$x = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $y = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); foreach ($x as $key => $value) { foreach ($y as $k => $v) { echo $value * $v . " "; } }
Note: $x can hold any number of values and so as $y.
If you dont know how many vectors or arrays you need to multiply try like below:
function multiply_arrays() { $args = func_get_args(); $number_of_args = count($args); for ($i = 0; $i < ($number_of_args - 1); $i++) { if (is_array($multiplied_array)) { $multiplied_array = multiply_two_arrays($multiplied_array, $args[$i]); } else { $multiplied_array = multiply_two_arrays($args[$i], $args[$i + 1]); } } return $multiplied_array; } function multiply_two_arrays($x, $y) { $multi_array = array(); foreach ($x as $key => $value) { foreach ($y as $k => $v) { $multi_array[] = $value * $v; } } return $multi_array; } $x = array(1, 2, 3); $y = array(1, 2, 3); $z = array(1, 2, 3); $a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $b = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $c = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); echo "<pre>"; print_r(multiply_arrays($x, $a, $b, $c)); echo "</pre>";
Thanks