0) { $ans[$i][$j] = (int) floor($ans[$i][$j] / $cnt); } } } return $ans; } public static function imageSmoother2(array $arr): array { $res = [[]]; if (empty($arr)) { return $res; } foreach ($arr as $i => $item) { foreach ($item as $j => $v) { $tmp = []; // current row $tmp[] = $v; if (isset($item[$j - 1])) { $tmp[] = $item[$j - 1]; } if (isset($item[$j + 1])) { $tmp[] = $item[$j + 1]; } // previous row if (isset($arr[$i - 1][$j])) { $tmp[] = $arr[$i - 1][$j]; } if (isset($arr[$i - 1][$j - 1])) { $tmp[] = $arr[$i - 1][$j - 1]; } if (isset($arr[$i - 1][$j + 1])) { $tmp[] = $arr[$i - 1][$j + 1]; } // next row if (isset($arr[$i + 1][$j])) { $tmp[] = $arr[$i + 1][$j]; } if (isset($arr[$i + 1][$j - 1])) { $tmp[] = $arr[$i + 1][$j - 1]; } if (isset($arr[$i + 1][$j + 1])) { $tmp[] = $arr[$i + 1][$j + 1]; } $tmp = (int) floor(array_sum($tmp) / count($tmp)); $res[$i][$j] = $tmp; } } return $res; } }