我正在使用PHP使用array_shift删除大约40000个CSV的第一行我遇到了以下错误:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /Applications/MAMP/htdocs/SeeClickFix/Finished_before_shift/shift.php on line 10
脚本如下所示:
<?php
// Read the file
for ($x = 0; $x <= 40000; $x++) {
$file = fopen('export'.$x.'.csv', 'r');
// Iterate over it to get every line
while (($line = fgetcsv($file)) !== FALSE) {
// Store every line in an array
$data[] = $line;
}
fclose($file);
// Remove the first element from the stored array / first line of file being read
array_shift($data);
// Open file for writing
$file = fopen('export'.$x.'.csv', 'w');
// Write remaining lines to file
foreach ($data as $fields) {
fputcsv($file, $fields);
}
fclose($file);
echo $x;
}
?>
这里的任何帮助都会非常感激,因为我从未见过这样的事情。
谢谢!
答案 0 :(得分:0)
在读取每个文件之前,您需要清空$data
数组。否则,它会累积所有文件中的行。
<?php
// Read the file
for ($x = 0; $x <= 40000; $x++) {
$file = fopen('export'.$x.'.csv', 'r');
$data = array();
// Iterate over it to get every line
while (($line = fgetcsv($file)) !== FALSE) {
// Store every line in an array
$data[] = $line;
}
fclose($file);
// Remove the first element from the stored array / first line of file being read
array_shift($data);
// Open file for writing
$file = fopen('export'.$x.'.csv', 'w');
// Write remaining lines to file
foreach ($data as $fields) {
fputcsv($file, $fields);
}
fclose($file);
echo $x;
}
?>
但是如果您只想从每个文件中删除第一行,则无需将其解析为CSV。你可以这样做:
<?php
for ($x = 0; $x <= 40000; $x++) {
$file = fopen('export'.$x.'.csv', 'r');
fgets($file); // Read first line
$lines = stream_get_contents($file); // Read remaining lines
fclose($file);
file_put_contents('export'.$x.'.csv', $lines);
echo $x;
}
?>