If you already have data as two separate arrays, merging them first will be a waste of time and resources that I could imagine.
There are two types of array access in PHP, iterative, which uses internal pointers and sequentially accesses through the associated key / index, which is a hash map, not a sequence. If you are going to look at all the elements of an array, and this can be done in order, try iteratively and then using either the built-in array_ functions or the iterator functions reset (), next (), cur (), end (), each ().
Take a look at the array_reduce () function in PHP, this can help you quickly achieve these kinds of things. Although in this simple case, you might be better off doing a direct for () loop and using the reset (), next (), cur () array iterator functions to get values ββfrom each array, or if they are entered equally with the key, you can simply execute foreach () and use the key from one to the other.
$sum_x = array_reduce($x, create_function('$x1,$x2', 'return $x1 + $x2;'), 0); $sum_y = array_reduce($y, create_function('$y1,$y2', 'return $y1 + $y2;'), 0); $sum_x2 = array_reduce($x, create_function('$x1,$x2', 'return $x1 + $x2 * $x2;'), 0); $sum_y2 = array_reduce($y, create_function('$y1,$y2', 'return $y1 + $y2 * $y2;'), 0);
or
$sum_x = 0; $sum_y = 0; $sum_x2 = 0; $sum_y2 = 0; foreach (array_keys($x) as $i) { $sum_x += $x[$i]; $sum_y += $y[$i]; $sum_x2 += $x[$i] * $x[$i]; $sum_y2 += $y[$i] * $y[$i]; }