如何使用PHP将行旋转为列?

我有多行这样的数组:

username => 'john', type = 'smtp'
username => 'john', type = 'pop'
username => 'james', type = 'pop'
username => 'james', type = 'smtp'

我想以这样的字符串显示数据:

john, smtp, pop
james, smtp, pop

也就是说,我需要多种类型以列的形式添加到每个用户名。

注意:$ accounts是一个数组数组。

$customers = array();

$pos = 0;
foreach ($accounts as $account) {
      if (array_key_exists('username', $account)) {
          $customers[$pos] .= "," . $account[type];
         }
         else
         {
          $customers[$pos] = $account[username] . "," . $account[type];   
         }

         $pos = $pos + 1;
         echo $pos;


}

print_r($customers);

You will have to excuse me as I am pretty new to PHP and my syntax might not perfect. I'm not sure if am using array_key_exists correctly.

评论