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

preferences:
26.9 ms | 508 KiB | 5 Q