PHP 获取唯一参数

发布于 2024-11-16 13:32:48 字数 440 浏览 3 评论 0原文

我有这个文本文件:

1 - word
1 - trata
2 - love
2 - green
2 - omg
3 - hello

如何解析该文件以获得以下内容?

1 - word, 1 - trata
2 - love, 2 - green, 2 - omg
3 - hello

即同一行上的相同数字。

<?php
    $file = file('1.txt');

    foreach ($file as $dd)
    {
        list ($numb, $word) = explode(' - ', $dd); // That's all (
        //echo $numb . '<br/>';
    }
?>

I have this text file:

1 - word
1 - trata
2 - love
2 - green
2 - omg
3 - hello

How to parse this file so that I get the following?

1 - word, 1 - trata
2 - love, 2 - green, 2 - omg
3 - hello

I.e. the same numbers on the same line.

<?php
    $file = file('1.txt');

    foreach ($file as $dd)
    {
        list ($numb, $word) = explode(' - ', $dd); // That's all (
        //echo $numb . '<br/>';
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晰传感 2024-11-23 13:32:48

像这样的东西(未经测试):

$file = file('1.txt');
$parse = array();

foreach ($file as $dd)
{
    list ($numb, $word) = explode(' - ', $dd);
    $parse[ $numb ][] = $dd;
}

foreach( $parse as $line ) {
    echo implode( ', ', $line ).'<br />';
}

Something like this (untested):

$file = file('1.txt');
$parse = array();

foreach ($file as $dd)
{
    list ($numb, $word) = explode(' - ', $dd);
    $parse[ $numb ][] = $dd;
}

foreach( $parse as $line ) {
    echo implode( ', ', $line ).'<br />';
}
怪我闹别瞎闹 2024-11-23 13:32:48
sort($file);
foreach ($file as $dd)
{
   list ($numb, $word) = explode(' - ', $dd); // That's all (
   if ($previous != $numb && $previous != null)
   {
       echo "<br>";
   }
   echo $numb ."-".$word ;       
   $previous=$numb;
}
sort($file);
foreach ($file as $dd)
{
   list ($numb, $word) = explode(' - ', $dd); // That's all (
   if ($previous != $numb && $previous != null)
   {
       echo "<br>";
   }
   echo $numb ."-".$word ;       
   $previous=$numb;
}
伏妖词 2024-11-23 13:32:48
$lines = file('test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$map = array(); // Keys will be your "prefix numbers", values will be the lines from the file.

foreach($lines as $line)
{
    if(!preg_match('/^(\d+)/', $line, $match))
        continue;

    $map[$match[1]][] = $line;
}

// Now echo the result.

foreach($map as $n => $lines)
{
    echo implode(', ', $lines), '<br />';
}

// That's all.
$lines = file('test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$map = array(); // Keys will be your "prefix numbers", values will be the lines from the file.

foreach($lines as $line)
{
    if(!preg_match('/^(\d+)/', $line, $match))
        continue;

    $map[$match[1]][] = $line;
}

// Now echo the result.

foreach($map as $n => $lines)
{
    echo implode(', ', $lines), '<br />';
}

// That's all.
凉城 2024-11-23 13:32:48
<?php
$file = file('1.txt');
$output = array( );
foreach( $file as $line ) {
    list( $key, $value ) = explode( " - ", $line );
    $output[ $key ][ ] = trim( $value );
}
foreach( $output as $key => $values ) {
    foreach( $values as $value ) {
        echo $key . " - " . $value . ", ";
    }
    echo "\n";
}
<?php
$file = file('1.txt');
$output = array( );
foreach( $file as $line ) {
    list( $key, $value ) = explode( " - ", $line );
    $output[ $key ][ ] = trim( $value );
}
foreach( $output as $key => $values ) {
    foreach( $values as $value ) {
        echo $key . " - " . $value . ", ";
    }
    echo "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文