我正在尝试将此数组的内容分发到一个新数组中,新数组键必须只包含 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
)
)
)
我很困惑。
答案 0 :(得分: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
)
)
)
答案 1 :(得分:0)
好像你将$ctr
与数字4匹配,这只有在$ctr
在比较点恰好等于4时才有效。由于$ctr
每个循环可能会增加1个以上,因此它可能会超过4个。
尝试:
if ($ctr >= 4)
代替。
答案 2 :(得分: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'];
}
为我工作