fork(7) download
  1. <?php
  2.  
  3. $numbers = array(1,3,2,23,24,25,26,8);
  4.  
  5. $result = array();
  6.  
  7. $sorted = $numbers;
  8. sort($sorted);
  9.  
  10. $current = null;
  11. foreach ($sorted as $v) {
  12. if (is_null($current)) {
  13. $current = array($v, $v);
  14. } else {
  15. if ($current[1] + 1 == $v) {
  16. $current[1] = $v;
  17. } else {
  18. $result[] = $current;
  19. $current = array($v, $v);
  20. }
  21. }
  22. }
  23.  
  24. $result[] = $current;
  25.  
  26. $arranged = array();
  27.  
  28. foreach ($numbers as $v) {
  29. foreach ($result as $k => $r) {
  30. if ($v >= $r[0] && $v <= $r[1]) {
  31. $arranged[] = $r;
  32. unset($result[$k]);
  33. break;
  34. }
  35. }
  36. }
  37.  
  38. var_dump($arranged);
Success #stdin #stdout 0.02s 13064KB
stdin
Standard input is empty
stdout
array(3) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(3)
  }
  [1]=>
  array(2) {
    [0]=>
    int(23)
    [1]=>
    int(26)
  }
  [2]=>
  array(2) {
    [0]=>
    int(8)
    [1]=>
    int(8)
  }
}