3

我运行查询并fetchAll()

$query = "SELECT uid, kill_count FROM users WHERE curmid=:mid AND status='1'";
...
$row = $stmt->fetchAll();

返回此数组:

Array ( 
 [0] => Array ( [uid] => 105 [fcount] => 1 ) 
 [1] => Array ( [uid] => 106 [fcount] => 2 ) 
 [2] => Array ( [uid] => 107 [fcount] => 0 ) 
 [3] => Array ( [uid] => 108 [fcount] => 1 ) 
 [4] => Array ( [uid] => 109 [fcount] => 1 ) 
)  

我有一个要运行的循环,基本上是:

$problist = array();
foreach ($row as $key => $value) {
    if ($value == 0) {
        array_push($problist, $key, $key, $key, $key, $key);
    } elseif ($value == 1) {
        array_push($problist, $key, $key, $key);
    } elseif ($value >= 2) {
        $problist[] = $key;
    }
}

当然,该循环不起作用,因为它没有引用数组中的正确值。在这个假设中,我正在寻找的输出是: $problist =

array(15) { 
[0]=> string(3) "105" 
[1]=> string(3) "105" 
[2]=> string(3) "105" 
[3]=> string(3) "106" 
[4]=> string(3) "107" 
[5]=> string(3) "107" 
[6]=> string(3) "107" 
[7]=> string(3) "107" 
[8]=> string(3) "107" 
[9]=> string(3) "108" 
[10]=> string(3) "108" 
[11]=> string(3) "108"
[12]=> string(3) "109" 
[13]=> string(3) "109" 
[14]=> string(3) "109" 
}  

我已经阅读和修改,然后阅读和修改无法弄清楚要使用哪些 php 函数来完成这项工作。我试过end(), reset(), array_shift(), 和其他的。当然有可能在我的无知中我没有正确使用它们。谢谢您的帮助。

[也许有更好的方法来格式化我的查询?]

4

2 回答 2

3
$problist = array();
foreach ($row as $value) {
    if ($value['fcount'] == 0) {
        array_push($problist, $value['uid'], $value['uid'], $value['uid'], $value['uid'], $value['uid']);
    } elseif ($value['fcount'] == 1) {
        array_push($problist, $value['uid'], $value['uid'], $value['uid']);
    } elseif ($value['fcount'] >= 2) {
        $problist[] = $value['uid'];
    }
}
于 2013-04-18T20:05:50.213 回答
1

您正在遍历整个结果集,因此$value数组看起来像:

$value = array(
    'uid' => 105,
    'fcount' => 1
);

因此,在您的代码中,您应该使用$value['uid']而不是$key$value['fcount']而不是$value.

于 2013-04-18T20:01:44.053 回答