Php 如何并行读取多个文件?
我有很多文本文件需要打开,然后在PHP中分配一些字段来设置字符串,最终写入MySQL 每个txt文件对app_id都有相同的唯一值,例如 所以文本文件1 应用程序id 名称 描述 文本文件2已被删除 应用程序id 类别 文本文件3已被删除 应用程序id 价格 我想获得每个记录的名称、描述、类别和价格,并将其写入mysql 我可以使用以下代码阅读第一个:Php 如何并行读取多个文件?,php,mysql,import,io,Php,Mysql,Import,Io,我有很多文本文件需要打开,然后在PHP中分配一些字段来设置字符串,最终写入MySQL 每个txt文件对app_id都有相同的唯一值,例如 所以文本文件1 应用程序id 名称 描述 文本文件2已被删除 应用程序id 类别 文本文件3已被删除 应用程序id 价格 我想获得每个记录的名称、描述、类别和价格,并将其写入mysql 我可以使用以下代码阅读第一个: $fp = fopen('textfile1','r'); if (!$fp) {echo 'ERROR: Unable to open fil
$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);
?>
$fp=fopen('textfile1','r');
如果(!$fp){echo'错误:无法打开文件。';退出;}
而(!feof($fp)){
$line=stream\u get\u line($fp,4096,$EOLDimiter);
如果($line[0]=='#')继续;//跳过以#开头的行
$field[$loop]=分解($delimiter,$line);
列表($app_id,$name,$description)=分解($delimiter,$line);
$fp++;
}
fclose($fp);
?>
我的问题是如何读取第二个和第三个文件?我是否可以并行执行,或者我是否需要完成文本文件1的阅读,将每一行写入mysql,然后打开文本文件2,并使用app_id键上的replace将类别写入mysql,然后对文本文件3执行相同的操作并打开?我建议以下方法:
- 创建一个包含以下字段的简单类:名称、描述、类别和价格
- 创建一个空数组,该数组将使用app_id进行索引,每个数组对应于上述类的一个实例
- 读取每个文件并将结果影响到数组中的正确元素
如果(!isset($array[$app\u id])$array[$app\u id]=new MyClass()代码>
array[$app\u id]->name=$name代码>
array[$app\u id]->description=$description代码>
- 读取完所有文件后,可以遍历数组并将每个元素写入SQL表
- 创建一个包含以下字段的简单类:名称、描述、类别和价格
- 创建一个空数组,该数组将使用app_id进行索引,每个数组对应于上述类的一个实例
- 读取每个文件并将结果影响到数组中的正确元素
如果(!isset($array[$app\u id])$array[$app\u id]=new MyClass()代码>
array[$app\u id]->name=$name代码>
array[$app\u id]->description=$description代码>
- 读取完所有文件后,可以遍历数组并将每个元素写入SQL表
- 我建议采用以下方法:
可能是在从每个流中读取一行之前检查每个流上的feof,如果其中一个流的行数比其他流的行数先用完,则会以错误消息终止。只需按顺序打开所有三个文件,然后在while循环中有三个stream\u get\u line,每个文件一个: $fp1=fopen('textfile1','r') $fp2=fopen('textfile2','r') $fp3=fopen('textfile3','r') 而(!feof($fp1)){ $line1=流获取线($fp1…) $line2=流获取线($fp2…) $line3=流取线($fp3…) 不过,您必须注意每个文件的行数完全相同。最好 可能是在从每个流读取一行之前检查每个流上的feof,如果其中一个流的行数比其他流的行数先用完,则会以错误消息中止操作。尝试以下操作:
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
)
)
file1.txt内容:
id 13类别测试描述测试
file2.txt:
id 13名称测试描述测试
ID15名称测试描述测试
ID17名称测试描述测试
$files=array('file1.txt','file2.txt');
foreach($files作为$file){
冲洗();
preg_match_all(“/id\s(?\d+)\s(?:name | category | price)?\s(?[\w]+)\s描述\s(?[^\n]+)/i”,@file_get_contents($file),$match);
打印(匹配);
}
返回:
大堆
(
[0]=>阵列
(
[0]=>ID13类别测试描述测试
)
[id]=>阵列
(
[0] => 13
)
[1] =>阵列
(
[0] => 13
)
[名称]=>数组
(
[0]=>测试
)
[2] =>阵列
(
[0]=>测试
)
[说明]=>阵列
(
[0]=>测试
)
[3] =>阵列
(
[0]=>测试
)
)
大堆
(
[0]=>阵列
(
[0]=>ID13名称测试描述测试
[1] =>id 15名称测试描述测试
[2] =>ID17名称测试描述测试
)
[id]=>阵列
(
[0] => 13
[1] => 15
[2] => 17
)
[1] =>阵列
(
[0] => 13
[1] => 15
[2] => 17
)
[名称]=>数组
(
[0]=>测试
[1] =>测试
[2] =>测试
)
[2] =>阵列
(
[0]=>测试
[1] =>测试
[2] =>测试
)
[说明]=>阵列
(
[0]=>测试
[1] =>测试
[2] =>测试
)
[3] =>阵列
(
[0]=>测试
[1] =>测试
[2] =>测试
)
)
试试这个:
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
)
)
file1.txt内容:
id 13类别测试描述测试
file2.txt:
id 13名称测试描述测试