Specifically in line 4 the 2nd $foo[$j][$k] gets caught into memory and doesn't get reset.
What do you mean by that?
Is there a better way to sum one dimension of a multidimensional array?
As I understand it, you have a 3D array and wish to generate a 2D array where each element of its inner array is the sum of the elements of the inmost array of the 3D array. Based on your example, there may be a simplification:
for ($j = 0; $j < $z; $j++) {
for ($k = 0; $k <= $y; $k++) {
$foo[$j][$k] = array_sum($bar[$j][$k]);
}
}
I am wary of the $k <= $y condition though, since typically if you start the index from 0, you want a $k < $y check.