fork download
  1. <?php
  2. $numbers = array(
  3. 10,
  4. 20,
  5. 100,
  6. 30,
  7. 40,
  8. 104,
  9. 200,
  10. 202,
  11. 33
  12. );
  13. $bestValue1 = $numbers[0];
  14. $bsetValue2 = $numbers[1];
  15. $lastDif = abs($numbers[0] - $numbers[1]);
  16.  
  17. for ($i = 1; $i < count($numbers); $i++) {
  18.  
  19. for ($k = 0; $k < $i; $k++) {
  20.  
  21. $dif = abs($numbers[$k] - $numbers[$i]);
  22.  
  23. if ($dif == 0) {
  24. continue;
  25. }
  26. if ($dif < $lastDif) {
  27. $lastDif = $dif;
  28. $bestValue2 = $numbers[$k];
  29. $bestValue1 = $numbers[$i];
  30. }
  31. }
  32.  
  33. }
  34. $pair = array(
  35. $bestValue1,
  36. $bestValue2
  37. );
  38. print_r($pair);
  39.  
  40. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 202
    [1] => 200
)