2010-07-19 83 views
2

我有很多文本文件需要打开,然后分配特定的字段来设置PHP中的字符串,最终写入MySQL。如何并行读取多个文件?

每个txt文件确实有APP_ID相同的唯一值,例如

所以文本文件1具有

APP_ID 名 描述

文本文件2具有

APP_ID 类别

文本文件3有

APP_ID 价格

我想每个记录名称,描述,类别和价格,并编写到MySQL。

我可以阅读使用代码的第一个:

$fp = fopen('textfile1','r'); 
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} 
while (!feof($fp)) { 
$line = stream_get_line($fp,4096,$eoldelimiter); 
if ($line[0] === '#') continue; //Skip lines that start with # 
$field[$loop] = explode ($delimiter, $line); 
list($app_id, $name, $description) = explode($delimiter, $line); 
$fp++; 
} 

fclose($fp); 
?> 

我的问题是如何读取第二和第三文件?我可以以某种方式并行执行,或者我需要完成读取文本文件1,将每行写入mysql,然后打开文本文件2,然后使用app_id键上的替换将类别写入mysql,然后执行与文本文件3打开相同的操作和?

+0

PHP是功能性的语言在这个环节做的PHP看看并行:http://home.jayhaabee.nl/trac/ 或 HTTP://dev.juokaz。 com/php/parallel-processes-in-php – 2010-07-19 13:11:43

回答

0

我推荐以下方法:

  • 创建包含以下字段的一个简单的类:名称,描述,类别和价格
  • 创建一个空的阵列将与APP_ID进行索引,每个对应于上述类
  • 的实例读取每个文件并将结果影响到正确的元件阵列中

    if (!isset($array[$app_id])) $array[$app_id] = new MyClass();

    array[$app_id]->name = $name;

    array[$app_id]->description = $description;

  • 一旦你阅读完所有的文件,您可以通过遍历数组和写入每个元素的SQL表。

+0

这可以工作,但是用这个解决方案你需要缓冲数据。这并不是必须的,他可以一次打开三个文件并遍历所有这些文件。 – JeSuisse 2010-07-19 13:28:22

0

只需打开所有这三个文件的顺序,然后有三个stream_get_line在while循环,每一个文件:

$ FP1 =的fopen( 'textfile1', 'R');

$ fp2 = fopen('textfile2','r');

$ fp3 = fopen('textfile3','r');

而(!FEOF(FP1 $)){

$一号线= stream_get_line($ FP1 ...)

$ 2号线= stream_get_line($ FP2 ...)

$ 3号线= stream_get_line($ FP3 ...)

...

你必须要小心,尽管如此,每个文件的行数都完全相同。最好的 可能会在每个流中检查feof,然后再从中读取一行,如果其中一个流在其他行之前耗尽,则会中止并显示一条错误消息。

+0

thx,我不能保证文件具有相同的行数,所以看着php.net,我需要3个嵌套循环吗?这是来自php.net的示例代码 <? $ fp = fopen(“myfile.txt”,“r”); $ current_line = fgets($ fp); ($ feof($ fp)){//处理当前行 $ current_line = fgets($ fp);处理当前行 } fclose($ fp); ?> – kitenski 2010-07-19 13:39:50

+0

如果它们没有相同的行数,则在名称和描述,类别和价格之间没有1:1的映射关系,并将所有这些统一在一条记录中,并将其输入到一个记录中数据库表可能是一个坏主意,因为当你更新记录内容时它会给你带来麻烦。 如果您的三个文件中的数据之间没有1:1的映射关系,最好创建3个数据库表,使用app_id作为链接表的关键点,并使用三个while循环顺序填充它们。 – JeSuisse 2010-07-19 14:50:09

0

试试这个:

 file1.txt contents: 
      id 13 category test description test 
    file2.txt: 
     id 13 name test description test 
     id 15 name test description test 
     id 17 name test description test 


    $files = array('file1.txt','file2.txt'); 

      foreach($files as $file) { 
       flush(); 
       preg_match_all("/id\s(?<id>\d+)\s(?:name|category|price)?\s(?<name>[\w]+)\sdescription\s(?<description>[^\n]+)/i",@file_get_contents($file),$match); 
        print_r($match); 
      } 

returns: 
Array 
(
    [0] => Array 
     (
      [0] => id 13 category test description test 
     ) 

    [id] => Array 
     (
      [0] => 13 
     ) 

    [1] => Array 
     (
      [0] => 13 
     ) 

    [name] => Array 
     (
      [0] => test 
     ) 

    [2] => Array 
     (
      [0] => test 
     ) 

    [description] => Array 
     (
      [0] => test 
     ) 

    [3] => Array 
     (
      [0] => test 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [0] => id 13 name test description test 
      [1] => id 15 name test description test 
      [2] => id 17 name test description test 
     ) 

    [id] => Array 
     (
      [0] => 13 
      [1] => 15 
      [2] => 17 
     ) 

    [1] => Array 
     (
      [0] => 13 
      [1] => 15 
      [2] => 17 
     ) 

    [name] => Array 
     (
      [0] => test 
      [1] => test 
      [2] => test 
     ) 

    [2] => Array 
     (
      [0] => test 
      [1] => test 
      [2] => test 
     ) 

    [description] => Array 
     (
      [0] => test 
      [1] => test 
      [2] => test 
     ) 

    [3] => Array 
     (
      [0] => test 
      [1] => test 
      [2] => test 
     ) 

)