Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I run a query andfetchAll()

$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 =

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.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted
$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'];
    }
}
share|improve this answer
    
You didn't factor in fcount at all. –  David Apr 18 '13 at 20:09
    
Hehe... Copy/Paste sucks. :) –  dikirill Apr 18 '13 at 20:10
    
Much appreciated. You practically answered my othe question: stackoverflow.com/questions/16082020/… Feel free to actually answer it for credit. –  David Apr 18 '13 at 21:26

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

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.