按电子邮件域对数组中的数据进行排序和回显


Sort and echo data from array by email domain

我目前有一个数组,我需要按电子邮件域对其进行排序,以便将我拥有的数据发布到表中。这是我目前拥有的阵列(一个缩写示例):

Array
(
[0] => wlrb@yahoo.com:7:8.35
[1] => hcda@able.com:4:5.59
[2] => kkyhid@hotmail.com:3:9.29
[3] => dxrjmowf@able.com:4:6.67
[4] => ybldb@hotmail.com:8:22.84
[5] => rcbyn@widgets.com:7:14.80
[6] => ggxxpkl@able.com:6:8.36
[7] => lnmpapq@hotmail.com:5:18.67
[8] => opkmc@widgets.com:5:22.88
[9] => wnkue@able.com:10:18.68
[10] => mgbbuqc@bodge.com:2:19.67
[11] => vswmdkqtb@bodge.com:5:21.89

这是我目前用来做的代码

<?php
    $filename = "orderdata.txt";
   // Open the file
 $fp = @fopen($filename, 'r'); 
 // Add each line to an array
 if ($fp) {
 $array = explode("'n", fread($fp, filesize($filename)));
 }
 function pr($data)
 {
      print "<pre>";
      print_r($data);
      print "</pre>";
 }
 pr($array);
    ?>

我想以这种格式将数据发布到表格中(不能使用数据库):

+-----------------------------------------------+
|                     able.com                  |
+---------------+------------------+------------+
| andy@able.com |         1        |      20.30 |
| ed@able.com   |         4        |       5.05 |
+---------------+------------------+------------+
|                     bodge.com                 |
+---------------+------------------+------------+
| foo@bodge.com |         3        |     132.20 |
+---------------+------------------+------------+

第二列是订单金额,最后一列是价格。

有人能帮我完成任务吗?

$filename = "orderdata.txt";
$details =  file($filename);
$emails = array();
for($i = 0; $i < count($details);$i++) {
    $curr = explode(":", $details[$i]);
    $curr[0] = explode("@", $curr[0]);
    $emails[$curr[0][1]][] = array($curr[0][0], $curr[1], $curr[2]);
}
ksort($emails);
print "<table border=1 width=500>";
foreach($emails as $k => $v) {
    print "<tr><td colspan='3' align=center>".$k."</td></tr>";
    foreach($v as $value) {
        print "<tr><td>".$value[0]."</td><td>".$value[1]."</td><td>".$value[2]."</td></tr>";
    }
}
print "</table>";

这个?

$filename = "orderdata.txt";
// Open the file as array
$lines =  file($filename);
$newArray = array();
foreach($lines as $line)
{
  $parts = explode(":", $line);
  $email = explode("@", $parts[0]);
  $newArray[] = array(
          'email' => $parts[0],
          'emailDomain' => $email[1],
          'order' => $parts[1],
          'price' => $parts[2]
  );
}
print_r($newArray);

代码将提取您想要的部分,并将其放入新的数组中。您仍然需要使用array_multisort()或任何其他方式对其进行排序。也可以使用preg_match提取零件,但在这种情况下分解更简单。