user3678005
user3678005

Reputation: 55

Sum a specific column of data in a 2d array

How can I sum this array:

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
  )
)

I would like to get something like this:

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

PS: how can I remove [0], [1] and [2] ?

Upvotes: 0

Views: 75

Answers (2)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

It clearly indicates that its the output of a database query. And you have used $result_type = MYSQLI_BOTH on *_fetch_array function call.

If you really want to get the sum Just fetch the sum in sql.

SELECT sum(`DEB_Sum`) FROM ... 

Upvotes: 0

Nick F
Nick F

Reputation: 10142

$total = 0;
foreach ($items as $item)
{
    $total += $item['DEB_Sum'];
}

...where $items is your original array.

Upvotes: 1

Related Questions