I have an array of numbers: 16,17,19,19,20. I need to find the missing number / space (in this case it is 18 / one number, but it can be two numbers, for example. 16,17,20,21), and then I want to fill in the gap so that the rest of the array moves one (x) number up. This array may have more missing numbers (space), for example. 16,17,19,19,20,21,23,23. I have this loop, but there is a problem - see Comment:
<?php
$ar = array(16,17,19,19,20);
$action = false;
$new = array();
$temp = array();
foreach ( $ar as $k => $v ) {
if ( $k == 0 )
{
$new[] = $v;
}
elseif ( $k > 0 )
{
if ( end($new) + 1 == $v )
{
$new[] = $v;
}
elseif ( end($new) + 1 != $v )
{
$gap = $v - end($new) - 1 ;
$action = true;
if ( $action = true )
{
$temp[] = $v - $gap;
}
}
}
}
echo "<br>";
print_r ( $new );
echo "<br>";
print_r ( $temp );
therefore the result: array new ok
Array ( [0] => 16 [1] => 17 )
temp array not working
Array ( [0] => 18 [1] => 18 [2] => 18 )
should be 18,18,19
How is this scenario considered? Thank.
phpJs source
share