I'm searching for an algorithm to remove gaps between numbers. Example of my problem:
Here is a range of integers: [1,2,3,4,9,10,11,17...]
I need to make those numbers like this: [1,2,3,4,5,6,7,8...]
Can anyone provide me with a working example of PHP code to obtain such a result?
You should fetch min and max from an array and create the range,
$min = min($arr);
$max = max($arr);
print_r(range($min,$max));
You can make by using range
function as:
// given array 2,4,6 and 9 are missing.
$arr1 = array(1,3,5,7,8,10);
// construct a new array using range
function by giving min(given array)
and max(given array)
value
$arr2 = range(min($arr1),max($arr1));