user2666633
user2666633

Reputation: 340

How to manipulate a multidimentional array

I am having a problem with my data from database.

array from database looks like this.

array[0]=>array(

   [date]=>1980-09-21,
   [biller]=>joe,
   [name]=>daniel,
   [amount]=>300

    )

  [1]=>array(

   [date]=>1980-09-21,
   [biller]=>joe,
   [name]=>daniel,
   [amount]=>440

   )

  [2]=>array(

   [date]=>1980-09-21,
   [biller]=>joe,
   [name]=>micheal,
   [amount]=>690
  )

The above is a sample of how the result array looks like, now i will like to merge the array with same name [name]=>daniel and have just one array for each person but add the corresponding [amount].

So wilt the above array i should have just two one have [name]=>daniel with [amount] = (400 +690) and other array with [name] =>micheal.

I dont know if there is any way i could have queried the database to give me the result i want so i dont have to manipulate the result array.

The database looks like this:

 |id | date     |biller   |name   | amount|
  1  |2013-08-23|joe      |daniel | 100   |
  2  |2013-08-23|joe      |daniel | 200   |
  3  |2013-08-23|joe      |micheal| 768  |

so i want a table after quering the above databse to look like:

 |id | date     |biller   |name   | amount|
  1  |2013-08-23|joe      |daniel | 300   | //add (100+200)
  2  |2013-08-23|joe      |micheal| 768   |

Upvotes: 0

Views: 36

Answers (1)

Ende Neu
Ende Neu

Reputation: 15783

You can retrieve that kind of result pretty easily using SUM and grouping by the name (and probably biller, depending on your data):

SELECT 
  id,
  date,
  biller,
  name,
  SUM(amount) as amount
FROM 
  myTable
GROUP BY 
  name

Upvotes: 2

Related Questions