PHP Dynamically Create CSV: Skip the First Line of a CSV File

PHP dynamically create CSV: Skip the first line of a CSV file

As you are keeping track of the row number anyway, you can use continue to skip the rest of the loop for the first row.

For example, add this at the start of your while loop (just above $num = count($data)):

if($row == 1){ $row++; continue; }

There are other ways to do this, but just make sure that when you continue, $row is still being incremented or you'll get an infinite loop!

PHP Displaying CSV file, Skip the first row

$file = file("compress.zlib://".$filePath);

unset($file[0]);

foreach($file as $line){

list($lid,$lname,$lrevenue,$ctr) = explode(",", $line);
print $lid ." | ". $lname ." | ". $lrevenue ." | ". $ctr . "\n";
}

Removing first line of CSV in PHP?

Call fgetcsv one more time before the loop starts.

How to skip the first line of a CSV file and make the second line the header

I don't think there's an elegant way of doing it, but it can be done:

require "csv"

# Create a stream using the original file.
# Don't use `textmode` since it generates a problem when using this approach.
file = File.open "file.csv"

# Consume the first CSV row.
# `\r` is my row separator character. Verify your file to see if it's the same one.
loop { break if file.readchar == "\r" }

# Create your CSV object using the remainder of the stream.
csv = CSV.new file, headers: true

PHP skip lines while reading

The only way to scroll through a CSV that respects the particular CSV format is to invoke a parser. The fastest CSV parser is the built in fgetcsv. Thus, that's going to be fastest of all possible reliable methods:

function csv_slice($fp, $offset = 0, $length = 0) {
$i = 0;
while (false !== ($row = fgetcsv($fp))) {
if ($i++ < $offset) continue;
if (0 < $length && $length <= ($i - $offset - 1)) break;
yield $row;
}
}

Used like:

$fp = fopen('file.csv', 'r');
print_r(
iterator_to_array(
csv_slice($fp, 5, 2) // start after 5th row, return 2 rows
)
);
fclose($fp);

I've used generators here to keep the memory consumption low. You can easily replace with a temporary array if you prefer.

If you can make some assumptions about line endings, then you can just read byte-by-byte until you found the range of line endings. But, frankly, I wouldn't do that.

my php code is only importing the first line of my csv file to the database

You have to loop over each line to read the whole file, otherwise you're only reading the first line in the file

`

$file = fopen($path.''.$filename.'.csv', "r");
$csv = array();
while (($csv = fgetcsv($file, 0, ",", ' ')) !== FALSE)
{
$n[] = $csv;
}
fclose($file);

`

Move first row from a csv to another using php

As easy as this ;-)

$old_file = 'testdata.csv';
$new_file = 'testdata_new.csv';

$file_to_read = file_get_contents($old_file); // Reading entire file
$lines_to_read = explode("\n", $file_to_read); // Creating array of lines

if ( $lines_to_read == '' ) die('EOF'); // No data

$line_to_append = array_shift( $lines_to_read ); // Extracting first line

$file_to_append = file_get_contents($new_file); // Reading entire file

if ( substr($file_to_append, -1, 1) != "\n" ) $file_to_append.="\n"; // If new file doesn't ends in new line I add it

// Writing files
file_put_contents($new_file, $file_to_append . $line_to_append . "\n");
file_put_contents($old_file, implode("\n", $lines_to_read));


Related Topics



Leave a reply



Submit