找到数字数组中的差距并填补缺口 - 循环

时间:2012-04-12 19:28:37

标签: php arrays

我有数字数组:16,17,19,19,20。 我需要找到一个缺失的数字/间隙(在这种情况下它是18 /一个数字,但它可能是两个数字,例如 16,17,20,21)然后我想填补空隙,使阵列的其余部分向上移动一(x)个数字。 该阵列可以具有更多缺失的数字(间隙),例如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 ) 

数组temp不正常

    Array ( [0] => 18 [1] => 18 [2] => 18 ) 

它应该是18,18,19

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

2 个答案:

答案 0 :(得分:2)

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

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
)

告诉我,如果我遗失了什么。

答案 1 :(得分:0)

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

<?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'] );