3

私は番号配列を持っています:16、17、19、19、20。不足している数値/ギャップ(この場合は18 / 1つの数値ですが、16、17、20、21などの2つの数値の場合もあります)を見つける必要があります。次に、配列の残りの部分と同じようにギャップを埋めたいと思います。 1つ(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 );

したがって、結果は次のようになります。配列newは問題ありません

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

アレイの温度は問題ありません

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

18,18,19である必要があります

このシナリオはどのように対処されますか?ありがとう。

4

2 に答える 2

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
)

何か不足している場合は教えてください。

于 2012-09-21T09:21:50.390 に答える
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'] );
于 2012-09-25T02:38:44.417 に答える