skip n rows of csv file in php -
i have csv file have 500000 number of rows. need take first 100 rows in first loop , manipulate rows (say, send first 100 ids api , save response). in second loop, skip first 100 rows(already taken) , take 100 rows , send request web service. similarly, in third loop, skip first 200 rows , take 100 rows , send request web service , on...
i can take single each rows below code. (tested : works great)
if (($handle = fopen($filename, "r")) !== false) { $id = 1; $line = fgetcsv($handle); //skip first row //fetch data each row while (($data = fgetcsv($handle, ",")) !== false) { $hotel_id = $data[0]; //call service request web service $hoteldetailrequest = (new \services\hotel\hotel)->gethotelstaticdata($hotel_id); //do stuff response } }
similarly, can skip initial rows skipped first row adding
$line = fgetcsv($handle); $line = fgetcsv($handle); $line = fgetcsv($handle);
but, not expected result explained above. using php(laravel). googled, not found suitable match criteria. has face same problem?
any appreciated. thank you
here solution you:
<?php $filename = "the_data.csv"; $chunk_size = 200; // parse csv file array $csv_data = array_map('str_getcsv', file($filename)); // split data array chunks $chunked_data = array_chunk($csv_data, $chunk_size); foreach($chunked_data $chunk){ // here have $chunk_size row data // iterate in chunk foreach($chunk $row ){ $hotel_id = $row[0]; // send request web service // stuff response } sleep(1); } ?>
Comments
Post a Comment