0

我正在尝试将此数组的内容分配到一个新数组中,新数组键必须总共只包含4 个计数,其中hit = 0等于1 个计数hit = 1等于2 个计数

从分配给变量的数据库中获取原始数组$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

似乎您正在与$ctr数字 4 匹配,这仅在$ctr比较时恰好等于 4 时才有效。由于$ctr每个循环可能会增加超过 1,因此它可能会超过 4。

尝试:

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 回答