I have this:
Array (
[0] => Array ( [f_count] => 1 [uid] => 105 )
[1] => Array ( [f_count] => 0 [uid] => 106 )
[2] => Array ( [f_count] => 2 [uid] => 107 )
[3] => Array ( [f_count] => 0 [uid] => 108 )
[4] => Array ( [f_count] => 1 [uid] => 109 )
[5] => Array ( [f_count] => 0 [uid] => 110 )
[6] => Array ( [f_count] => 3 [uid] => 111 )
)
What I need is: 7
", which is the the sum of the f_count
column.
I've been trying to figure this out for a couple hours. I thought array_sum()
would work, but not with a multidimensional array. So, I've tried figuring out how to isolate the f_count
s by unset()
or splicing or anything else, but every solution seems to involve a foreach
loop. I've messed with array_map
, array_walk
, and others to no avail. I haven't found a function that works well with multidimensional arrays.
I'm running PHP 5.4.
Can someone please show me how to sum that column without a foreach
loop?
If it helps, the f_count
values will never be higher than 100
, and the uid
values will always be greater than 100
.
Alternatively, if there's a way to run my query differently such that the array is not multidimensional, that would obviously work as well.
$query = "SELECT f_count, uid FROM users WHERE gid=:gid";
...
$array = $stmt->fetchAll();
I'm using PDO.
question from:
https://stackoverflow.com/questions/16138395/sum-values-of-multidimensional-array-by-key-without-loop 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…