I’m working on an array of numeric values.
I have a array of numeric values as the following in PHP
11,12,15,16,17,18,22,23,24
And I’m trying to convert it into range for e.g in above case it would be:
11-12,15-18,22-24
I don’t have any idea how to convert it into range.
If we have previous item and current item is not next number in sequence, then we put previous range (start-prev) in output array and current item will be start of next range, if we don't have previous item, then this item is the first item and as mentioned before - first item starts a new range. newItem function returns range or sigle number if there is no range. If you have unsorted array with repeating numbers, use sort() and array_unique() functions.
1-5,7,9-12,15-16