get csv in php

skip first line or any line in csv file using php

how to skip first line or any line in csv file using php

In this post, we will show you how to skip headers or skip 1st or any row of import CSV in PHP. By using this code we can skip multiple roe of csv file. we have to add roe number $skip_row_number and pass skip row number in code. so we will check if row meach then skip row.

 // count row number 
$row = 0;
// add you row number for skip 
// hear we pass 1st row for skip in csv 
$skip_row_number = array("1");

// open csv file 
$csv_file = fopen("contacts.csv","r");

while(($data = fgetcsv($csv_file, 1000, ",")) !== FALSE) {
	$row++;	
	// count total filed of csv row 
    $num = count($data);  
    // check row for skip row 	
	if (in_array($row, $skip_row_number))	
    {
		continue; 
		// skip row of csv
	}
	else
	{
		// print row number and total filed of csv
		echo "<p> $num fields in line $row: <br /></p>\n";
		$csv_data[] = $data;
		// add data in to array 
	}    
}
// close csv file 
fclose($csv_file);

// print csv data 
// csv data is in array
echo "<pre>";
print_r($csv_data);
echo "</pre>";

Leave a Comment

Your email address will not be published. Required fields are marked *

6  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US