【问题标题】:Sort and echo data from array by email domain按电子邮件域对数组中的数据进行排序和回显
【发布时间】:2014-11-02 16:11:51
【问题描述】:

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

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 |
+---------------+------------------+------------+

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

有人可以帮我完成任务吗?

【问题讨论】:

  • 你需要使用file('orderdata.txt');而不是你做的你需要通过':'分解数据来获取部分字符串。然后,您需要正则表达式从邮件中获取域,或者在更简单的版本中通过“@”邮件爆炸,您需要使用 array_multisort() 对数组进行排序
  • @Robert 你能进一步解释一下吗?我很难解决这个问题:(
  • 我会试着给你写一些代码
  • @Robert 显然我需要使用关联数组来传递数据,我的头很痛!

标签: php arrays sorting html-table


【解决方案1】:
$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>";

这个?

【讨论】:

  • 是的,但是如何从外部文件“orderdata.txt”将数据绘制到数组中?我还需要将域按字母升序排列,但目前让它们正确打印更为重要。编辑:我现在已经从 .txt 文件中打印了数组,对不起,愚蠢的时刻。我只需要按升序打印域。
  • 编辑:我现在已经从 .txt 文件中打印了数组,对不起,愚蠢的时刻。我只需要按升序打印域。
  • 这里没有正确打印代码,我将添加为注释。
  • 现在退一步哈哈,sort($emails); 已经用数字替换了 'yahoo.com' 等..
  • 这很奇怪,也许只是因为我使用的是限制性更强的 PHP 版本或其他什么东西。我不知道。非常感谢您的帮助!
【解决方案2】:

$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 提取零件,但在这种情况下,explode 更简单。

【讨论】:

  • 返回几个错误.... Notice: Undefined offset: 1 in C:\xampp\htdocs\PhpProject3\index.php on line 26 Notice: Undefined offset: 1 in C:\xampp\htdocs\PhpProject3\index.php on line 27 Notice: Undefined offset: 2 in C:\xampp\htdocs\PhpProject3\index.php on line 28 那么我的数组看起来像这样... Array ( [0] =&gt; Array ( [email] =&gt; [emailDomain] =&gt; [order] =&gt; [price] =&gt; ) [1] =&gt; Array ( [email] =&gt; aciohord@able.com [emailDomain] =&gt; able.com [order] =&gt; 1 [price] =&gt; 21.61 ) [2] =&gt; Array ( [email] =&gt; afspqpq@bodge.com [emailDomain] =&gt; bodge.com [order] =&gt; 3 [price] =&gt; 13.22 )
  • 我不知道您做错了什么,但我将代码放入小提琴中并且它可以工作sandbox.onlinephpfunctions.com/code/… 您遇到的错误是因为您的数据格式与您提供的问题不同。它们需要排成一行,每一行都用换行符分隔
猜你喜欢
  • 2015-05-17
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 2014-01-07
  • 2010-12-21
  • 2013-01-07
相关资源
最近更新 更多