扯掉我的头发...我如何对以下数组中的数字求和,尝试了一下,但是没有用。我什至在循环内尝试了它,但我什么也没得到。我不认为这应该那么困难,但由于某种原因我无法理解。
$sql = "SELECT queue_name,type,COUNT(uniqueid) AS calls FROM CallLog WHERE start_time BETWEEN '2013-10-14 00:00:00' AND '2013-10-14 23:59:59' GROUP BY queue_name, type";
$stmt=$dbh->prepare($sql);
$stmt->execute();
$calls = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($calls as $call){
$results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];
}
$totalCalls = array_sum($call['calls']);
echo '<pre>';
print_r($results);
echo '</pre>';
echo $totalCalls;
?>
Array
(
[Escalations] => Array
(
[abandoned] => 2
[completed] => 3
[redirected] => 1
)
[Premium] => Array
(
[abandoned] => 7
[completed] => 29
[redirected] => 6
)
[Standard] => Array
(
[abandoned] => 14
[completed] => 41
[redirected] => 53
)
[Wingate Queue] => Array
(
[abandoned] => 2
[completed] => 3
)
[WorldMark] => Array
(
[abandoned] => 32
[completed] => 100
[redirected] => 82
)
)
最佳答案
您的代码中有一个错误:
$totalCalls = array_sum($call['calls']);
// $call['calls'] is just a single value not an array
最简单的方法是:
$totalCalls = 0;
foreach($calls as $call){
$results[ $call['queue_name'] ][ $call['type'] ] = $call['calls'];
$totalCalls += $call['calls'];
}