0


重複がある場合、インデックスと値を 5 番目のカウントに移動する配列が必要です。また、値が整数であるか英数字であるかにかかわらず、フィールド値を使用します。

これがアイデアです:

<?php

$array  =  array(
                 array('value' => 1),
                 array('value' => **2**),
                 array('value' => **2**), //Move this
                 array('value' => **2**), //Move the also second duplicate
                 array('value' => A),
                 array('value' => B),
                 array('value' => C),
                 array('value' => 6),
                 array('value' => **7**),
                 array('value' => **7**), //Move this
                 array('value' => 8),
                 array('value' => 9),
                 array('value' => 10),
                 array('value' => 11)
                );
?>


値 5 と 7 を 5 回目の反復ごとに移動したいと考えています。

これが結果に違いない。

<?php

$array  =  array(
                 array('value' => 1),
                 array('value' => **2**),
                 array('value' => A),
                 array('value' => B),
                 array('value' => C),
                 array('value' => **2**), //Move to fifth array, row
                 array('value' => 6),
                 array('value' => **7**),
                 array('value' => 8),
                 array('value' => **2**), //Move to the fifth array if has second duplicate
                 array('value' => 9),
                 array('value' => 10),
                 array('value' => **7**), //Move to fifth array, row
                 array('value' => 11)
                );
?>


数週間でこれに問題があります。これに可能なコードはありますか?誰でも私を助けることができますか?

4

3 に答える 3

1

質問を 100% 理解しているかどうかはわかりませんが、次のコードで必要な出力が生成されます。

$lastvalue= null;
$toinsert= null;
$insertcount= 0;
$insertindex= -1;
$result= array();

for($ndx=0;$ndx<count($array);$ndx++)
{
    $value= $array[$ndx]['value'];
    if ($value==$lastvalue) 
    {
        // It's a duplicate, set the insert index
        $insertindex= $ndx+4; // 4 places on from here (5 from the first occurrence)
        // Store the value we want to duplicate
        $toinsert= $array[$ndx];
        $insertcount++;
    }
    elseif(isset($toinsert) && $value==$toinsert['value']) {
        // We're already dealing with one of these, add this to the count to be
        // inserted
        $insertcount++;
    }
    else 
    {
        if ($ndx==$insertindex)
        {
            // We've reached the insertindex, insert our duplicate(s)
            for($i=0;$i<$insertcount;$i++)
            {
                $result[]= $toinsert;
            }
            $insertcount= 0;
        }
        $result[]= $array[$ndx];
        $lastvalue= $value; // Store this value so we can check for duplicates in the next iteration
    }
}

print_r($result);

基本的に、私が行ったのは、元の配列を反復して複製を探すことだけです。重複が見つかった場合は、重複値を記録し、その値を挿入するインデックスを計算します (現在のインデックス + 4 - これは、重複の元の発生からの 5 回の反復です!)。そのインデックスに到達すると、保存された値が結果に挿入されます。

最初の挿入ポイントを見つける前に 2 番目の重複のセットが見つかった場合、これは機能しないことに注意してください (最初の重複を破棄し、2 番目の重複を処理します)。また、挿入ポイントに到達する前に元の配列の最後に到達した場合、複製を再挿入しません。

それはまったく役に立ちますか?

編集複数の連続した重複を処理できるように更新されました。これは、ヒットしたときに挿入する必要があるレコード数のカウントを維持することによって行われます$insertindex

注: このコードは現在、少し醜い側になっています。これは、既に提案されている他の回答の 1 つであり、複数の重複シナリオにより適している可能性があります。

于 2013-10-09T11:40:42.160 に答える