PHP与PDO fetch_assoc按特定值排序/分组

时间:2013-08-14 14:13:45

标签: php arrays sorting pdo grouping

我有一个来自PDO Fetch_assoc的var_dumped数组,如:

array
  0 => 
    array
      'Client_Firstname' => string 'Sandrah' (length=...)
      'Client_Lastname' => string 'Lewis' (length=...)
      'Client_Email' => string 'san@lewis.com' (length=...)
      'Purchased_ItemId' => int '16' (length=...)
      'ItemName' => string 'MiniPad' (length=...)
      'ItemPrice' => string '500.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)
  1 => 
     array
      'Client_Firstname' => string 'Derick' (length=...)
      'Client_Lastname' => string 'Smith' (length=...)
      'Client_Email' => string 'der@smith.com' (length=...)
      'Purchased_ItemId' => int '10' (length=...)
      'ItemName' => string 'MegaPad' (length=...)
      'ItemPrice' => string '1500.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)
  2 => 
     array
      'Client_Firstname' => string 'Sandrah' (length=...)
      'Client_Lastname' => string 'Lewis' (length=...)
      'Client_Email' => string 'san@lewis.com' (length=...)
      'Purchased_ItemId' => int '18' (length=...)
      'ItemName' => string 'Xpower Speakers' (length=...)
      'ItemPrice' => string '300.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)
  3 => 
     array
      'Client_Firstname' => string 'Sandrah' (length=1)
      'Client_Lastname' => string 'Lewis' (length=7)
      'Client_Email' => string 'san@lewis.com' (length=7)
      'Purchased_ItemId' => int '7' (length=7)
      'ItemName' => string 'Lion Belt' (length=7)
      'ItemPrice' => string '800.00' (length=7)
      'ItemType' => string 'Fashion' (length=7)
      'date' => string '2013-12-12' (length=7)
  4 => 
    array
      'Client_Firstname' => string 'Sandrah' (length=1)
      'Client_Lastname' => string 'Lewis' (length=7)
      'Client_Email' => string 'san@lewis.com' (length=7)
      'Purchased_ItemId' => int '27' (length=7)
      'ItemName' => string 'FT Card Reader' (length=7)
      'ItemPrice' => string '20.00' (length=7)
      'ItemType' => string 'Hardware' (length=7)
      'date' => string '2013-12-12' (length=7)
  5 => 
     array
      'Client_Firstname' => string 'Jonathan' (length=1)
      'Client_Lastname' => string 'Homad' (length=7)
      'Client_Email' => string 'jon@homad.com' (length=7)
      'Purchased_ItemId' => int '20' (length=7)
      'ItemName' => string 'MiniPad' (length=7)
      'ItemPrice' => string '500.00' (length=7)
      'ItemType' => string 'Hardware' (length=7)
      'date' => string '2013-12-12' (length=7)

目标是,在输出中,每个客户的名字,姓氏和电子邮件应该只出现一次,然后是所有的交易。

类似的东西:

array
  0 => 
    array
      'Client_Firstname' => string 'Sandrah' (length=...)
      'Client_Lastname' => string 'Lewis' (length=...)
      'Client_Email' => string 'san@lewis.com' (length=...)
      'Purchased_ItemId' => int '16' (length=...)
      'ItemName' => string 'MiniPad' (length=...)
      'ItemPrice' => string '500.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)
      'Purchased_ItemId' => int '18' (length=...)
      'ItemName' => string 'Xpower Speakers' (length=...)
      'ItemPrice' => string '300.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)
      'Purchased_ItemId' => int '7' (length=...)
      'ItemName' => string 'Lion Belt' (length=...)
      'ItemPrice' => string '800.00' (length=...)
      'ItemType' => string 'Fashion' (length=...)
      'date' => string '2013-12-12' (length=...)
      'Purchased_ItemId' => int '27' (length=...)
      'ItemName' => string 'FT Card Reader' (length=...)
      'ItemPrice' => string '20.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)

  1 => 

     array
      'Client_Firstname' => string 'Derick' (length=...)
      'Client_Lastname' => string 'Smith' (length=...)
      'Client_Email' => string 'der@smith.com' (length=...)
      'Purchased_ItemId' => int '10' (length=...)
      'ItemName' => string 'MegaPad' (length=...)
      'ItemPrice' => string '1500.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)

  2 => 

     array
      'Client_Firstname' => string 'Jonathan' (length=...)
      'Client_Lastname' => string 'Homad' (length=...)
      'Client_Email' => string 'jon@homad.com' (length=...)
      'Purchased_ItemId' => int '20' (length=...)
      'ItemName' => string 'MiniPad' (length=...)
      'ItemPrice' => string '500.00' (length=...)
      'ItemType' => string 'Hardware' (length=...)
      'date' => string '2013-12-12' (length=...)

根据我的每个数组中唯一唯一的标识符是电子邮件地址。因此,我尝试使用for循环,foreach循环尝试根据电子邮件地址对它们进行分组,在其他SO相关问题的指导下,但这还没有实现。

非常感谢任何帮助。

0 个答案:

没有答案