# [FIXED] range of numbers from array

## Issue

I have an array of sorted numbers, for example:

``````Array (
 => 33
 => 34
 => 35
 => 36
 => 66
 => 67
 => 68
 => 69
 => 89
 => 90
 => 91
 => 92
 => 93
)
``````

In this case, we have the following ranges of numbers:

1) 33-36

2) 66-69

3) 89-93

I want to create an array for each range:

``````Array1 (  => 33  => 34  => 35  => 36 )

Array2 (  => 66  => 67  => 68  => 69 )

Array3 (  => 89  => 90  => 91  => 92  => 93 )
``````

## Solution

``````<?php

\$array = [33,34,35,36,66,67,68,69,89,90,91,92,93];

\$min = \$array;
\$currentRange = 0;
\$ranges = [];

foreach (\$array as \$element) {
if(\$min+1 < \$element) {
\$currentRange++;
}

\$ranges[\$currentRange][] = \$element;
\$min = \$element;
}

var_dump(\$ranges);
``````

Output:

``````array(3) {
=>
array(4) {
=>
int(33)
=>
int(34)
=>
int(35)
=>
int(36)
}
=>
array(4) {
=>
int(66)
=>
int(67)
=>
int(68)
=>
int(69)
}
=>
array(5) {
=>
int(89)
=>
int(90)
=>
int(91)
=>
int(92)
=>
int(93)
}
}
``````

Answered By – Pyton

Answer Checked By – Clifford M. (Easybugfix Volunteer) 