私は番号配列を持っています: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である必要があります
このシナリオはどのように対処されますか?ありがとう。