对二维数组中的特定数据列求和[重复]

问题描述 投票:0回答:2

如何对这个数组求和:

Array (
  [0] => Array (
    [DEB_Sum] => 1
    [0] => 1
    [DEB_MovementDate] => 2014-06-13
    [1] => 2014-06-13
    [INV_Name] => Chèque bancaire
    [2] => Chèque bancaire
  )
  [1] => Array (
    [DEB_Sum] => 0.18
    [0] => 0.18
    [DEB_MovementDate] => 2014-06-15
    [1] => 2014-06-15
    [INV_Name] => Argent comptant
    [2] => Argent comptant
  )
)

我想要得到这样的东西:

// Sum of [DEB_Sum]
$sum = 1.18;

PS:如何删除[0]、[1]和[2]?

php mysql arrays multidimensional-array sum
2个回答
1
投票
$total = 0;
foreach ($items as $item)
{
    $total += $item['DEB_Sum'];
}

...其中

$items
是您的原始数组。


0
投票

它清楚地表明它是数据库查询的输出。并且您已在

$result_type = MYSQLI_BOTH
 函数调用中使用了 
*_fetch_array

如果你真的想得到总和只需在sql中获取总和即可。

SELECT sum(`DEB_Sum`) FROM ... 
© www.soinside.com 2019 - 2024. All rights reserved.