duanmi4379 2014-09-05 09:37
浏览 54

解析制表符分隔文件,然后生成数组

I have a tab delimited file which I'm parsing, I then need to parse the resulting arrays.

This is how I'm parsing the file:

$fp = fopen('myFile.txt', 'r');

while ( !feof($fp) )
{
  $line = fgets($fp, 2048);

  $delimiter = "\t";
  $array = str_getcsv($line, $delimiter);
} 

To check the data I try print_r($array); which gives me for example:

Array ( [0] => Customer Name [1] => Customer Email [2] => Date [3] => Test Date)
Array ( [0] => John Smith [1] => john@gmail.com [2] => Date [3] => 01/01/1985)

How can I parse this? I'm trying the following but it produces a blank page.

foreach($array as $a) {
    echo $a.'<br>';
}

I'm quite new to arrays but can clearly see nothing is enclosed in quotes which I guess is causing me a problem. Should the quotes be added during the parsing of the tab delimited file? If so how?

  • 写回答

1条回答

  • doutan3463 2014-09-05 09:41
    关注

    The problem might be your use of !feof: you're checking if you haven't already reached the end of the file, and that's fine, but you're not checking the return value of fgets. The last line in a file often is just a new-line character, so on your last iteration, it's not unlikely that $line ends up being an empty string.
    You're also re-assigning $array, each time replacing the previous values you assigned to it. If the loop you posted is situated outside of the while loop, it's not unlikely that what you're doing is actually this:

    $line = '';//as explained, $line might be an empty string
    $array = str_getcsv($line, "\t");//which is $array = array();
    

    If you then attempt to lop over this empty array, you're not going to echo anything:

    foreach (array() as $a)
        echo $a;//there are no values to echo...
    

    Your best bet is to construct a 2D array:

    $array = array();
    while(...)
        $array[] = $lineAsArray;
    

    This is pseudo-code, because the while-loop you have can be simplified a lot:
    Why not use the fgetcsv function with a custom delimiter?

    $fh = fopen('myFile.txt', 'r');
    if (!$fh)
        exit(1);//handle error
    while ($line = fgetcsv($fh, 2048, "\t"))
    {//line is an array of CSV values now
        echo implode('<br>', $line), '<br>';//echo all values
    }
    

    That's all there is too it. Instead of implode you can loop over line, but seeing as you're just echoing all values, followed by <br>, I figured using implode just made more sense.

    I don't know for sure what your file looks like, but the longest line might be longer than 2048 bytes, you can take advantage of the fact that PHP can handle a 0 length argument. If the length is zero, the line will be read until an EOL, or EOF is encountered:

    while ($line = fgetcsv($fh, 0, "\t"))
    

    That'll work just fine...

    Bottom line:
    I think that you're looking for something along the lines of:

    $fh = fopne('myFile.txt', 'r');
    if (!$fh)
        exit(1);//handle error
    $array = array();
    $alternative = array();
    while($line = fgetcsv($fh, 0, "\t"))
    {
        $array[] =  $line;
        //or if you simply want to echo:
        $alternative[] = implode('<br>', $line);
    }
    //the short alternative
    echo implode('<br>', $alternative);
    //the long way:
    foreach ($array as $line)
    {
        echo implode('<br>',$line), '<br>';
    }
    

    Both amount to the ssame thing

    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员