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);
Output for 8.1.9
<pre>Array ( [1] => Array ( [0] => 0 [1] => 4 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 2 ) ) [3] => Array ( [1] => Array ( [0] => 2 [1] => 4 ) ) ) </pre>
Output for 8.1.8
<pre>Array ( [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 3 ) ) ) </pre>
Output for 8.0.13, 8.0.15, 8.1.7
<pre>Array ( [0] => Array ( [0] => 2 [1] => 3 ) ) </pre>
Output for 7.4.1, 7.4.20, 8.0.12, 8.0.22, 8.1.6
<pre>Array ( [0] => Array ( [0] => 1 [1] => 2 ) ) </pre>
Output for 7.4.3, 8.1.5
<pre>Array ( [0] => Array ( [0] => 1 [1] => 4 ) ) </pre>
Output for 8.0.11, 8.1.4
<pre>Array ( [0] => Array ( [0] => 1 [1] => 3 ) ) </pre>
Output for 7.4.26, 8.1.3
<pre>Array ( [1] => Array ( [0] => 1 [1] => 3 ) ) </pre>
Output for 8.1.2
<pre>Array ( [1] => Array ( [0] => 0 [1] => 3 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 2 ) ) [3] => Array ( [1] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>
Output for 8.1.1
<pre>Array ( [1] => Array ( [0] => 0 [1] => 1 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 3 ) ) [3] => Array ( [1] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>
Output for 8.1.0
<pre>Array ( [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>
Output for 8.0.21
<pre>Array ( [2] => Array ( [0] => 0 [1] => 4 ) [3] => Array ( [2] => Array ( [0] => 1 [1] => 3 ) ) [4] => Array ( [2] => Array ( [0] => 2 [1] => 4 ) ) ) </pre>
Output for 8.0.20
<pre>Array ( [0] => Array ( [0] => 0 [1] => 3 ) [1] => Array ( [0] => Array ( [0] => 1 [1] => 2 ) ) ) </pre>
Output for 7.4.4, 7.4.19, 8.0.19
<pre>Array ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [0] => Array ( [0] => 1 [1] => 4 ) ) [2] => Array ( [0] => Array ( [0] => 2 [1] => 4 ) ) ) </pre>
Output for 7.4.22, 8.0.18
<pre>Array ( [1] => Array ( [0] => 0 [1] => 1 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 4 ) ) [3] => Array ( [1] => Array ( [0] => 2 [1] => 3 ) ) ) </pre>
Output for 7.4.8, 7.4.24, 8.0.17
<pre>Array ( [0] => Array ( [0] => 0 [1] => 3 ) ) </pre>
Output for 8.0.16
<pre>Array ( [1] => Array ( [0] => 0 [1] => 3 ) [2] => Array ( [1] => Array ( [0] => 0 [1] => 4 ) ) ) </pre>
Output for 7.4.6, 8.0.6, 8.0.14
<pre>Array ( [0] => Array ( [0] => 1 [1] => 3 ) [1] => Array ( [0] => Array ( [0] => 1 [1] => 4 ) ) [2] => Array ( [0] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>
Output for 8.0.10
<pre>Array ( [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 4 ) ) ) </pre>
Output for 7.4.0, 8.0.8 - 8.0.9
<pre>Array ( [0] => Array ( [0] => 0 [1] => 1 ) ) </pre>
Output for 7.4.12, 7.4.21, 8.0.7
<pre>Array ( [0] => Array ( [0] => 0 [1] => 2 ) ) </pre>
Output for 7.4.13, 7.4.18, 7.4.30, 8.0.5
<pre>Array ( [0] => Array ( [0] => 2 [1] => 4 ) ) </pre>
Output for 8.0.3
<pre>Array ( [1] => Array ( [0] => 0 [1] => 1 ) ) </pre>
Output for 8.0.2
<pre>Array ( [1] => Array ( [0] => 0 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 2 [1] => 3 ) ) ) </pre>
Output for 8.0.1
<pre>Array ( [1] => Array ( [0] => 0 [1] => 4 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 2 ) ) [3] => Array ( [1] => Array ( [0] => 1 [1] => 3 ) ) ) </pre>
Output for 7.4.29
<pre>Array ( [1] => Array ( [0] => 0 [1] => 2 ) ) </pre>
Output for 7.4.28
<pre>Array ( [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 2 [1] => 3 ) ) ) </pre>
Output for 7.4.27
<pre>Array ( [0] => Array ( [0] => 0 [1] => 2 ) [1] => Array ( [0] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>
Output for 7.4.9, 7.4.25
<pre>Array ( [0] => Array ( [0] => 0 [1] => 4 ) ) </pre>
Output for 7.4.23
<pre>Array ( [0] => Array ( [0] => 1 [1] => 4 ) [1] => Array ( [0] => Array ( [0] => 2 [1] => 3 ) ) ) </pre>
Output for 7.4.16
<pre>Array ( [1] => Array ( [0] => 0 [1] => 3 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 2 ) ) ) </pre>
Output for 7.4.15
<pre>Array ( [0] => Array ( [0] => 0 [1] => 2 ) [1] => Array ( [0] => Array ( [0] => 1 [1] => 3 ) ) ) </pre>
Output for 7.4.14
<pre>Array ( [2] => Array ( [0] => 0 [1] => 2 ) [3] => Array ( [2] => Array ( [0] => 1 [1] => 4 ) ) ) </pre>
Output for 7.4.11
<pre>Array ( [1] => Array ( [0] => 0 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 0 [1] => 3 ) ) ) </pre>
Output for 7.4.10
<pre>Array ( [1] => Array ( [0] => 0 [1] => 1 ) [2] => Array ( [1] => Array ( [0] => 0 [1] => 2 ) ) [3] => Array ( [1] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>
Output for 7.4.7
<pre>Array ( [0] => Array ( [0] => 1 [1] => 3 ) [1] => Array ( [0] => Array ( [0] => 2 [1] => 4 ) ) ) </pre>
Output for 7.4.5
<pre>Array ( [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [1] => Array ( [0] => 1 [1] => 3 ) ) [3] => Array ( [1] => Array ( [0] => 2 [1] => 4 ) ) ) </pre>
Output for 7.4.2
<pre>Array ( [1] => Array ( [0] => 0 [1] => 1 ) [2] => Array ( [1] => Array ( [0] => 3 [1] => 4 ) ) ) </pre>

preferences:
39.7 ms | 481 KiB | 18 Q