如何操作一个多维数组


How to manipulate a multidimentional array

我有一个问题,我的数据从数据库。

数组从数据库看起来像这样。

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
  )

以上是结果数组的一个示例,现在我想合并同名的数组[name]=>daniel,每个人只有一个数组,但添加相应的[amount]。

那么上面的数组应该只有两个吗?一个是[name]=>daniel with [amount] =(400 +690),另一个是[name]=> micheal.

我不知道是否有任何方式我可以查询数据库给我我想要的结果,所以我不必操纵结果数组。

数据库是这样的:

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

所以我想要查询上面数据库后的表看起来像:

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

您可以很容易地使用SUM和按名称分组(可能还有biller,取决于您的数据)检索这种结果:

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