instead of this: $m_avrg[$a][$b] = ($m_avrg[$a][$b] + $list[$a][$b]); use this: $m_avrg[$a][$b] += $list[$a][$b];