2012-04-12 195 views
3

我有数组数组: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) 

阵列温度是不正常

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

它应该是18,18,19

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

+0

我之前解决过,所以我不得不刷新我的记忆。 – phpJs 2012-09-25 00:21:56

回答

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

只要告诉我如果我失去了一些东西。

+0

你的结果增加了一个数字,所以数组现在有6个成员,但最初的数组只有5个成员。所以你的解决方案在这种情况下不好。我会发布函数(它是递归)我做了什么。 – phpJs 2012-09-25 01:40:04

+0

我有一个想法之前...你需要删除任何重复的值? – akhyar 2012-09-25 05:42:24