找到数字数组中的间隙并填充间隙 - 循环


find the gap in numbers array and fill the gap - loop

我有数字数组:16,17,19,19,20。我需要找到一个缺失的数字/间隙(在这种情况下是 18/一个数字,但它可能是两个数字,例如16,17,20,21),然后我想以一种数组的其余部分向上移动一 (x) 个数字的方式填补空白。这个数组可以有更多的缺失数字(gap),例如16,17,19,19,20,21,23,23。我有这个循环,但有问题 - 见评论:

    <?php
      $ar = array(16,17,19,19,20);
                $action = false;
                $new = array();
                $temp = array();
        foreach ( $ar   as $k => $v ) {
                if ( $k == 0 )
                    {
                      // case 0 - insert first value of var into array - never need to change
                        $new[] = $v; 
                    }
                elseif ( $k > 0 ) 
                    { 
                      if ( end($new) + 1 == $v ) 
                        { 
                      // case 1 - numbers are consequence/no gap - insert into array - no change 
                            $new[] = $v;
                        }
                  elseif ( end($new) + 1 != $v )
                            {
                              // case 2 - there is a gap: find the size of the gap (1,2,x) and then subtract all next values of array with the gap
                                    $gap = $v - end($new) - 1 ; // find value of the gap 
                                    //echo "<br> gap is: " . $gap; // PROBLEM - gap get increased by every loop but i need to keep gap size static and process rest of the array
                                    $action = true;
                                            if ( $action = true )
                                                {    
                                                        $temp[] = $v - $gap;
                                                }
                                        }
                            }
                        }                            
echo "<br>";            
print_r ( $new );
echo "<br>";
print_r ( $temp );

所以结果是:数组新没问题

    Array ( [0] => 16 [1] => 17 ) 

阵列温度不正常

    Array ( [0] => 18 [1] => 18 [2] => 18 ) 
它应该是 18,

18,19

如何解决这种情况?谢谢。

阅读您的目标和当前关于填充缺失数字的代码,我想出了自己的代码:

http://pastebin.com/58nNGuNB

$ar = array(16,17,19,19,20);
$action = false;
$new = array();
$temp = array();
foreach ($ar as $key => $val) {
    if($key == 0 OR $ar[$key-1] == $val) {
        // if it's first value, or current value is same as previous, just add it as is
        $new[] = $val;
    } else {
        // else, add all missing value including current one to $new array
        for($x = $ar[$key - 1] + 1; $x <= $val; $x++) {
            $new[] = $x;
        }
    }
}

其输出如下:

Array
(
    [0] => 16
    [1] => 17
    [2] => 18
    [3] => 19
    [4] => 19
    [5] => 20
)

如果我错过了什么,请告诉我。

试试这个,你可以做任意数量的间隙和序列,递归会找到间隙的大小,向上移动数字,然后寻找另一个间隙/间隙。

<?php
$ar = array(16,17,17,19,19,20,20,24,24);
echo '<pre>Initial:<br>';
print_r ($ar);
echo '<br>Fixed:<br>';
function find_gaps($array) {  
                $key = 0;
                $first_half = array();
                $gap = 0;                   
                $gaps = array();
                $action = false;
                $new = array();  // helper array
                $second_half = array();
    foreach ( $array as $k => $v ) {
                if ( $k == 0 )  // first number in line - no need to change
                    {
                        $new[] = $v;  
                        $first_half[$k] = $v;
                    }
                elseif ( $k > 0 )  // next numbers 
                    { 
                      if ( end($new) + 1 == $v ) // if first member of array is consequent of helper .. 
                          { 
                            $new[] = $v;        
                            $first_half[$k] = $v;  // .. it's ok- just make new array
                            }
                      elseif ( end($new) == $v )  // if there are two or more same numbers - it's ok ..
                                { 
                            $new[] = $v;
                            $first_half[$k] = $v;  // .. no action-just keep adding into array 
                           }
                    elseif ( end($new) + 1 != $v )  // if last value of helper array is not 1 less of our array - there is a gap
                                {
                                        $gap = $v - end($new) - 1 ; // find size of the gap in numbers ( can be 1,2,3..)
                                        //echo $gap; break;
                                        $gaps[] = $gap;  // put gap into array - it will get increased but you need only first value - see if ($action)
                                        $action = true;
                                }   
                                    if ( $action )
                                            { 
                                            $second_half[$k] = $v;  // make second half array
                                            } 
                  }
            } // end array  loop then..

    $second_half_fixed = array();
        foreach ( $second_half as $keys => $values )
                          $second_half_fixed[$keys] = $values - $gaps[0];
            $a['first']  = $first_half;
            $a['fixed'] = $second_half_fixed;
            $b['fixed'] = $first_half + $second_half_fixed;
            $b['gaps'] = $gaps;
            $count_gaps = count($b['gaps']);
            if ($count_gaps == 0 OR $count_gaps == 1 ){
                            return $b;
                }
            else // more gaps
                    {
                        echo 'run again<br>';
                          return ( find_gaps($b['fixed'])) ;            
                    }       
    } // end function 'find_gaps'
    $fixed = find_gaps($ar);
    print_r ( $fixed['fixed'] );