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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)