-3

I have this

array
    (
        [0] => 1976,
        [1] => 1977,
        [2] => 1978,
        [3] => 1979,
        [4] => 1980,
        [5] => 1981,
        [6] => 1982,
        [7] => 1983,
        [8] => 1990,
        [9] => 1991,
        [10] => 1993,
        [11] => 1994,
        [12] => 1995
    )

And i want to find the interval between gaps so it looks like this:

$tmp[1]= [[0] => 1976, [1] => 1983]

$tmp[1]= [[0] => 1990, [1] => 1991]

$tmp[1]= [[0] => 1993, [1] => 1995]

Eventually I'll be using the same method with largest list of number andy idea ?

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Jelqui
  • 11
  • 2
  • 4
    What are you having trouble with? – FirstOne Nov 10 '15 at 19:29
  • 1
    So, is this something you have tried to solve yourself? Or are you just expecting someone to do this for you? Typically you need to show some effort in at least trying it and people can help. – Jonathan Kuhn Nov 10 '15 at 19:36
  • @JonathanKuhn Yes I was trying to solve it by myself – Jelqui Nov 10 '15 at 20:02
  • 1
    Well, you should typically include what you have tried and where you think it is failing or what you are having trouble with. This will get you better responses to show an effort. Up to possibly someone just rewriting it or showing you how to do something better/different. See [ask] in the help center, specifically the section on `Help others reproduce the problem`. I hope I'm not coming off as rude (even the above comment). I'm honestly trying to help. – Jonathan Kuhn Nov 10 '15 at 20:12
  • @andrewsi Shouldn't be on hold I saw this post [similar format](http://stackoverflow.com/questions/7901851/find-gaps-in-a-list-of-numbers) before posting my and its almost looks like my. I thinks my point is to get the result using as any way possible that would work. Thank you – Jelqui Nov 11 '15 at 14:40

1 Answers1

1

Despite the fact that you didn't provide any code, here is something that could work assuming I understood correctly

    function getIntervals($array) {
       $intervals = [];
       $intervalIndex = 0;
       for ($i = 0; $i < count($array); $i++) {
          //If the beginning is not set, set it.
          if (!isset($intervals[$intervalIndex][0])) {
             $intervals[$intervalIndex][0] = $array[$i];
          }
          if ($i != 0) {
             if ($array[$i] == ($array[$i - 1] + 1)) {
                $intervals[$intervalIndex][1] = $array[$i];
             } else {
                $intervalIndex++;
                $intervals[$intervalIndex][0] = $array[$i];
             }
          }
       }
       return $intervals;
    }

Here is a usage example with your list:

getIntervals([
    1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983,
    1990, 1991,
    1993, 1994, 1995,
]);

and the expected result:

array:3 [
  0 => array:2 [
    0 => 1976
    1 => 1983
  ]
  1 => array:2 [
    0 => 1990
    1 => 1991
  ]
  2 => array:2 [
    0 => 1993
    1 => 1995
  ]
]
Christoph Kluge
  • 1,947
  • 8
  • 23
Ali Hamze
  • 1,590
  • 13
  • 26