解析多维数组的PHP函数?

时间:2022-05-16 15:39:38

I run a query andfetchAll()

我运行查询和fetchall ()

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

that returns this array:

返回这个数组:

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 ) 
)  

I have a loop that I want to run it through that is basically:

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

$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;
    }
}

Of course, that loop doesn't work as it doesn't reference the proper values in the array. The output I'm looking for, in this hypothetical, is: $problist =

当然,这个循环不起作用,因为它没有引用数组中的正确值。在这个假设中,我要查找的输出是:$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" 
}  

I've read and tinkered and then read and tinkered cannot figure out which php functions to use to make this work. I've tried end(), reset(), array_shift(), and others. It is certainly possible that in my ignorance I wasn't using them correctly. Thanks for the help.

我已经阅读和修改过,然后阅读和修改,不知道该用哪个php函数来完成这项工作。我尝试过end()、reset()、array_shift()等。毫无疑问,在我的无知下,我没有正确地使用它们。谢谢你的帮助。

[Perhaps there is even a better way to format my query?]

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

2 个解决方案

#1


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'];
    }
}

#2


1  

You are looping through entire resultset, so $value is an array looking like:

您正在循环遍历整个resultset,因此$value是一个看起来像:

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

So in your code you should use $value['uid'] instead of $key and $value['fcount'] instead of $value.

所以在代码中,你应该使用$value['uid']而不是$key和$value['fcount']而不是$value。

#1


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'];
    }
}

#2


1  

You are looping through entire resultset, so $value is an array looking like:

您正在循环遍历整个resultset,因此$value是一个看起来像:

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

So in your code you should use $value['uid'] instead of $key and $value['fcount'] instead of $value.

所以在代码中,你应该使用$value['uid']而不是$key和$value['fcount']而不是$value。