3v4l.org

run code in 300+ PHP versions simultaneously
<?php function debug($arr){ echo '<pre>' ; print_r($arr); echo '</pre>'; } for($i=0; $i<5; $i++) { $arr[$i] = rand(0,10); } // $arr = Array // ( // 0 => 7, // 1 => 2, // 2 => 9, // 3 => 9, // 4 => 0, // ); // print_r($arr) . PHP_EOL; debug($arr) . PHP_EOL; $size_arr = sizeof($arr); // debug($size_arr) . PHP_EOL; $diff = array_sum($arr); for ($i=0; $i < $size_arr; $i++) { $a = $arr[$i]; //echo 'echo $i - ' . $i . PHP_EOL; //$index = [$diff => [$i,$j]]; for( $j=$i+1; $j < $size_arr; $j++) { $b = $arr[$j]; $newdiff = abs($a-$b); // echo $a . ' - ' . $b . ' = ' . $newdiff . '<br>'; //echo '------- $diff ----- ' . $diff . PHP_EOL; //echo '------- $newdiff --- ' . $newdiff . PHP_EOL; if ($newdiff < $diff) { $index = [$newdiff => [$i,$j]]; $diff = $newdiff; } elseif ($newdiff == $diff) { $newvalues = [$newdiff => [$i, $j]]; array_push($index, $newvalues); } } } debug($index);

preferences:
32.18 ms | 421 KiB | 19 Q