3v4l.org

run code in 300+ PHP versions simultaneously
<?php // arrB to not get changes reflected once we sort $arrB = $array = [4, 1, 5, 52, 75, 52, 24]; function findMinDiff($arr) { $n = count($arr); // Initialize difference // as infinite $diff = PHP_INT_MAX; // Find the min diff by comparing // difference of all possible // pairs in given array $two_values = []; for ($i = 0; $i < $n - 1; $i++) { for ($j = $i + 1; $j < $n; $j++) { if (abs($arr[$i] - $arr[$j]) < $diff) { $diff = abs($arr[$i] - $arr[$j]); $two_values['values'] = [$arr[$i], $arr[$j]]; $two_values['keys'] = [$i, $j]; $two_values['diff'] = $diff; } } } // Return min diff return $two_values; } print_r(findMinDiff($array));

preferences:
31.48 ms | 405 KiB | 5 Q