dongxie8856 2014-09-09 09:34
浏览 46
已采纳

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

I currently have an array which I need to sort by email domain to post the data I have into a table. Here is the array I currently have (a shortened example) :

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

and here is the code I am using currently to do this

<?php
    $filename = "orderdata.txt";
   // Open the file
 $fp = @fopen($filename, 'r'); 

 // Add each line to an array
 if ($fp) {
 $array = explode("
", fread($fp, filesize($filename)));
 }
 function pr($data)
 {
      print "<pre>";
      print_r($data);
      print "</pre>";
 }
 pr($array);
    ?>

I would like to post the data into a table in this format(cant use a database):

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

The second column is order amount and the last column is price.

Could anyone help me in my quest?

  • 写回答

2条回答 默认 最新

  • doufanglian7585 2014-09-09 09:57
    关注
    $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>";
    

    This it?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大