我有一个2000行的文本文件,并且需要连续循环一次获得100行。我可以使用它,但是我必须为每100个块手动更改$ i值。这是代码:
$file = 'postcode_2000.txt';
for ($i = 0; $i <= 99; $i++) {
$str = str_replace(PHP_EOL, '', $file[$i]);
$list[] = $str;
}
$list_json = json_encode($list);
我该如何迭代每次获得下一个100?
答案 0 :(得分:1)
将文件存放在数组中后,就可以使用array_chunk()
。首先将file读入数组:
$lines = 'postcode_2000.txt';
然后chunk将文件分成100行:
$chunks = array_chunk($lines, 100, true); // true keeps a set of consecutive line numbers
输出将是这样的:
Array
(
[0] => Array
(
[0] => line in file
[1] => line in file
[2] => line in file
[3] => line in file
[4] => line in file
...
[94] => line in file
[95] => line in file
[96] => line in file
[97] => line in file
[98] => line in file
[99] => line in file
)
[1] => Array
(
[100] => line in file
[101] => line in file
[102] => line in file
[103] => line in file
[104] => line in file
...
[194] => line in file
[195] => line in file
[196] => line in file
[197] => line in file
[198] => line in file
[199] => line in file
)
[2] => Array
(
[200] => line in file
[201] => line in file
[202] => line in file
[203] => line in file
[204] => line in file
...
[294] => line in file
[295] => line in file
[296] => line in file
[297] => line in file
[298] => line in file
[299] => line in file
)
...
[18] => Array
(
[1800] => line in file
[1801] => line in file
[1802] => line in file
[1803] => line in file
[1804] => line in file
...
[1894] => line in file
[1895] => line in file
[1896] => line in file
[1897] => line in file
[1898] => line in file
[1899] => line in file
)
[19] => Array
(
[1900] => line in file
[1901] => line in file
[1902] => line in file
[1903] => line in file
[1904] => line in file
...
[1994] => line in file
[1995] => line in file
[1996] => line in file
[1997] => line in file
[1998] => line in file
[1999] => line in file
)
)
答案 1 :(得分:0)
如果您真的想使用循环而不是array_chunk:)
<?php
$file = file('/tmp/postcodes_2000.txt');
$list = array();
$line_count = count($file);
$chunk_size = 100;
$current_line = 0;
echo "Line count: ${line_count}" . PHP_EOL;
while( $current_line < $line_count ) {
$i = 0;
$chunk = array();
while($i < $chunk_size) {
$str = str_replace(PHP_EOL, '', $file[$current_line]);
$chunk[] = $str;
echo "> Line ${current_line} : " . $file[$current_line] . PHP_EOL;
$i++;
$current_line++;
}
$list[] = $chunk;
}
$list_json = json_encode($list);
echo $list_json . PHP_EOL;
答案 2 :(得分:0)
生成器非常适合以下情况:当您正在计算大型集合时,您不想为所有结果同时分配内存,或者当您不知道是否需要所有结果时,通过这种方式处理结果,只需为当前结果分配内存,就可以将内存占用量减少到最低限度。
因此,这是一种非常快速的方法,它消耗更少的内存来实现您的目标:
我们打开文件,读取精确的行数,然后生成结果,同时保留下一次迭代的状态,从而使您可以非常快速地处理块,并减少内存消耗。
以这种方式进行操作的好处是,在最终允许您(第三个)之前,避免两次读取所有文件以构建块(一次读取以构建包含所有行的大数组,然后循环遍历该数组以构建块)。时间)遍历大块数组...
function file_get_x_lines($file,$amount=100){
if(!file_exists($file)||!is_file($file)||!is_readable($file))
return;
if(!is_int($amount)||$amount<1) $amount=1;
$handle=fopen($file,'rb');
$i=0;
while($line=fgets($handle)){
if($i===0)
$tmp=array();
$chunks[]=rtrim($line,PHP_EOL);
$i++;
if($i===$amount){
$i=0;
$tmp=$chunks;
$chunks=array();
yield $tmp;
}
}
if(!$line)
yield $chunks;
}
然后您可以像使用任何生成器一样使用它
foreach(file_get_x_lines(__FILE__) as $list) {
$list=json_encode($list);
//do stuff
}
如果您想保持与行号相同的键,可以通过以下方式轻轻更改功能:
function file_get_x_lines($file,$amount=100){
if(!file_exists($file)||!is_file($file)||!is_readable($file))
return;
if(!is_int($amount)||$amount<1) $amount=1;
$handle=fopen($file,'rb');
$i=0;
$j=1;
while($line=fgets($handle)){
if($i===0)
$tmp=array();
$chunks[$j]=rtrim($line,PHP_EOL);
$i++;
$j++;
if($i===$amount){
$i=0;
$tmp=$chunks;
$chunks=array();
yield $tmp;
}
}
if(!$line)
yield $chunks;
}