Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP functions to parse multidimensional array?

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?]

like image 904
David Avatar asked Sep 16 '26 01:09

David


1 Answers

$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'];
    }
}
like image 68
dikirill Avatar answered Sep 18 '26 17:09

dikirill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!