0

この配列の内容を新しい配列に分散しようとしています。新しい配列キーには、合計4カウントのみが含まれている必要があります。ここで、は1カウントhit = 0等しく、は2カウントに等しくなります。hit = 1

変数に割り当てられたデータベースからの生の配列フェッチ$ret

Array
(
    [0] => Array (
            [id] => 33, [hits] => 0
        )
    [1] => Array (
            [id] => 32, [hits] => 0
        )
    [2] => Array (
            [id] => 31, [hits] => 0
        )
    [3] => Array (
            [id] => 30, [hits] => 0
        )
    [4] => Array (
            [id] => 29, [hits] => 0
        )
    [5] => Array (
            [id] => 28, [hits] => 1
        )
    [6] => Array (
            [id] => 27, [hits] => 0
        )
    [7] => Array (
            [id] => 26, [hits] => 0
        )
    [8] => Array (
            [id] => 25, [hits] => 1
        )
    [9] => Array (
            [id] => 24, [hits] => 0
        )
    [10] => Array (
            [id] => 23, [hits] => 1
        )
    [11] => Array (
            [id] => 22, [hits] => 1
        )
    [12] => Array (
            [id] => 21, [hits] => 0
        )
    [13] => Array (
            [id] => 20, [hits] => 1
        )
    [14] => Array (
            [id] => 19, [hits] => 1
        )
    [15] => Array (
            [id] => 18, [hits] => 0
        )
    [16] => Array (
            [id] => 17, [hits] => 0
        )
    [17] => Array (
            [id] => 16, [hits] => 0
        )
    [18] => Array (
            [id] => 15, [hits] => 0
        )
    [19] => Array (
            [id] => 14, [hits] => 1
        )
    [20] => Array (
            [id] => 10, [hits] => 0
        )
    [21] => Array (
            [id] => 9, [hits] => 1
        )
    [22] => Array (
            [id] => 8, [hits] => 0
        )
    [23] => Array (
            [id] => 7, [hits] => 0
        )
    [24] => Array (
            [id] => 3, [hits] => 1
        )
)

私はこのコードを作成しました:

$new_arr = array();
$row = 0;
$ctr = 0;
foreach($ret as $ak1 => $av1) {
  if($ctr == 4) {
    $row++;
    $ctr = 0;
  }
  else {

  }
  if($av1['hits'] == 0) {
    $ctr++;
    $new_arr[$row][] = $av1;
  }
  else {
    $ctr+=2;
     $new_arr[$row][] = $av1;
  }
}
print_r($new_arr);

最初のいくつかのキー[0] [1] [2] [3]には適切な結果が含まれているようですが、キーから開始することはできません[4]

Array
(
    [0] => Array
        (
            [0] => Array (
                    [id] => 33
                    [hits] => 0
                )
            [1] => Array (
                    [id] => 32
                    [hits] => 0
                )
            [2] => Array (
                    [id] => 31
                    [hits] => 0
                )
            [3] => Array (
                    [id] => 30
                    [hits] => 0
                )
        )
    [1] => Array
        (
            [0] => Array (
                    [id] => 29
                    [hits] => 0
                )
            [1] => Array (
                    [id] => 28
                    [hits] => 1
                )
            [2] => Array (
                    [id] => 27
                    [hits] => 0
                )
        )
    [2] => Array
        (
            [0] => Array (
                    [id] => 26
                    [hits] => 0
                )
            [1] => Array (
                    [id] => 25
                    [hits] => 1
                )
            [2] => Array (
                    [id] => 24
                    [hits] => 0
                )
        )
    [3] => Array
        (
            [0] => Array (
                    [id] => 23
                    [hits] => 1
                )
            [1] => Array (
                    [id] => 22
                    [hits] => 1
                )
        )
    [4] => Array
        (
            [0] => Array (
                    [id] => 21
                    [hits] => 0
                )
            [1] => Array (
                    [id] => 20
                    [hits] => 1
                )
            [2] => Array (
                    [id] => 19
                    [hits] => 1
                )
            [3] => Array (
                    [id] => 18
                    [hits] => 0
                )
            [4] => Array (
                    [id] => 17
                    [hits] => 0
                )
            [5] => Array (
                    [id] => 16
                    [hits] => 0
                )
            [6] => Array (
                    [id] => 15
                    [hits] => 0
                )
            [7] => Array (
                    [id] => 14
                    [hits] => 1
                )
            [8] => Array (
                    [id] => 10
                    [hits] => 0
                )
            [9] => Array (
                    [id] => 9
                    [hits] => 1
                )
            [10] => Array (
                    [id] => 8
                    [hits] => 0
                )
            [11] => Array (
                    [id] => 7
                    [hits] => 0
                )
            [12] => Array (
                    [id] => 3
                    [hits] => 1
                )
        )
)

私はとても混乱しています。

4

3 に答える 3

1

あなたが抱えている問題は、未完成の各セットの「カウント」を追跡できる必要があるということです。これでうまくいきます。詳細はコードコメントにあります。

ご不明な点がございましたら、コメント欄でお尋ねください。必要に応じてこの投稿を編集します。

:これは、ヒットが常に0または1(カウント1または2)になることを前提としています。アイテムのヒット数が1を超える(カウントが2を超える)場合は、機能するように変更する必要があります。

編集:@fishcrackerによって引き起こされた問題に対処するために、コードと結果を更新しました。

$sets = array();
// Because it's possible to have more than one unfinished set
$not_full = array();
// Because we need to track each set's count until it's full
$set_template = array("count" => 0);
$set = $set_template;
$max = 4;
foreach($ret as $item) {
    $item_count = $item["hits"] + 1;

    // If we have a 1 count and there are sets to be topped off
    if($item_count == 1 && !empty($not_full)) {
        $top_off = array_shift($not_full);
        $top_off[] = $item;
        unset($top_off["count"]);
        $sets[] = $top_off;
        continue;
    }

    // If we're going to overflow, push this set to potentially be topped off later
    if($set["count"] + $item_count > $max) {
        $not_full[] = $set;
        $set = $set_template;
    }

    // Add the item to the current set
    $set[] = $item;
    $set["count"] += $item_count;

    // This set is full, 
    if($set["count"] == $max) {
        unset($set["count"]);
        $sets[] = $set;
        $set = $set_template;
    }
}

// If we left the loop with an unfinished set which we were actively working on
if(count($set > 1)) {
    unset($set["count"]);
    $sets[] = $set;
}

// Append any unfinished sets that were waiting to be full
foreach($not_full as $set) {
    unset($set["count"]);
    $sets[] = $set;
}

print_r($sets);

結果:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 33
                [hits] => 0
            )

        [1] => Array
            (
                [id] => 32
                [hits] => 0
            )

        [2] => Array
            (
                [id] => 31
                [hits] => 0
            )

        [3] => Array
            (
                [id] => 30
                [hits] => 0
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 29
                [hits] => 0
            )

        [1] => Array
            (
                [id] => 28
                [hits] => 1
            )

        [2] => Array
            (
                [id] => 27
                [hits] => 0
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 26
                [hits] => 0
            )

        [1] => Array
            (
                [id] => 25
                [hits] => 1
            )

        [2] => Array
            (
                [id] => 24
                [hits] => 0
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [id] => 23
                [hits] => 1
            )

        [1] => Array
            (
                [id] => 22
                [hits] => 1
            )

    )

[4] => Array
    (
        [0] => Array
            (
                [id] => 21
                [hits] => 0
            )

        [1] => Array
            (
                [id] => 20
                [hits] => 1
            )

        [2] => Array
            (
                [id] => 18
                [hits] => 0
            )

    )

[5] => Array
    (
        [0] => Array
            (
                [id] => 19
                [hits] => 1
            )

        [1] => Array
            (
                [id] => 17
                [hits] => 0
            )

        [2] => Array
            (
                [id] => 16
                [hits] => 0
            )

    )

[6] => Array
    (
        [0] => Array
            (
                [id] => 15
                [hits] => 0
            )

        [1] => Array
            (
                [id] => 14
                [hits] => 1
            )

        [2] => Array
            (
                [id] => 10
                [hits] => 0
            )

    )

[7] => Array
    (
        [0] => Array
            (
                [id] => 9
                [hits] => 1
            )

        [1] => Array
            (
                [id] => 8
                [hits] => 0
            )

        [2] => Array
            (
                [id] => 7
                [hits] => 0
            )

    )

[8] => Array
    (
        [0] => Array
            (
                [id] => 3
                [hits] => 1
            )

    )

)
于 2012-12-08T19:05:44.280 に答える
0

数字の4と一致しているようです。これは、比較の時点でが4に正確に等しい$ctr場合にのみ機能します。ループごとに1を超えて増加する可能性があるため、4を超える可能性があります$ctr$ctr

試す:

if ($ctr >= 4)

代わりは。

于 2012-12-08T17:16:42.973 に答える
0

私はあなたが何を意味するのか理解していると思いますが、100%ではありません、これはあなたが何をしようとしているのですか?

foreach($ret as $ak1 => $av1) {
    if($ctr >= 4){
         $row += 1;
         $ctr = 0;
    }
    if($ret[$ak1]['hits']){
        $ctr += 2;
    }
    else{
        $ctr += 1;
    }
    $new_arr[$row][$ret[$ak1]['id']] = $ret[$ak1]['hits'];
}

私のために働く

于 2012-12-08T17:27:06.450 に答える