1

我有如下文本文件:

文本.txt

lebuzzdesbonsplans.com;hotmail.com;26608;828;3.11
lebuzzdesbonsplans.com;hotmail.fr;24798;876;3.53
friendcorp.fr;yahoo.fr;11343;0;0
friendcorp.fr;free.fr;9856;12;.12
friendcorp.fr;wanadoo.fr;9283;1;.01
messengear.fr;free.fr;9090;11;.12
messengear.fr;laposte.net;8107;2;.02
....................................
....................................

PHP代码:

<?php
            $PMTA_FILE = file_get_contents("text.txt");
            $lineFromText = explode("\n", $PMTA_FILE);
            $title = "";
            $domain = "";
            foreach($lineFromText as $line){                    
                    $data = explode(";",$line);
                    $domain = $data[0];
                    if (array_key_exists($domain, $domains_seen)){
                        continue;

                    }
                 $domains_seen[$domain] = true; 
                 echo $domain;
                 echo "<br>";
                 echo $data[2]; 
                 echo "<br>";   
          }


?>

但是这段代码的结果:

lebuzzdesbonsplans.com 
26608
friendcorp.fr
11343
messengear.fr
9090

我不想要上面的结果,我需要如下的结果:

lebuzzdesbonsplans.com 
26608
24798
friendcorp.fr
11343
9856
9283
messengear.fr
9090
8107

请知道的人帮我解决一下,谢谢。

4

2 回答 2

1

You can use

$lines = array_reduce(file("text.txt", FILE_IGNORE_NEW_LINES), function ($a, $b) {
    $b = str_getcsv($b, ";");
    $a[$b[0]][] = $b[2];
    return $a;
});


foreach($lines as $k => $values)
{
    echo $k,PHP_EOL;
    echo implode(PHP_EOL, $values);
    echo PHP_EOL;
}

Output

lebuzzdesbonsplans.com
26608
24798
friendcorp.fr
11343
9856
9283
messengear.fr
9090
8107
于 2012-12-20T07:48:39.603 回答
0
natsort($lineFromText);
foreach($lineFromText as $line){                    
    $data = explode(";",$line);
    $domain = $data[0];
    if (!array_key_exists($domain, $domains_seen)){
        echo $domain;
        echo "<br>";
    }
    echo $data[2]; 
    echo "<br>";   
    $domains_seen[$domain] = true; 
}

更新:首先对行进行排序 - 这样所有域都将被收集在一起,

于 2012-12-20T07:43:22.077 回答