how to delete empty rows in laravel excel?

Mohammed Hassar picture Mohammed Hassar · Feb 24, 2015 · Viewed 7.1k times · Source

So i work with Laravel, and i use Laravel excel to load excel/csv files, but my files contains empty rows and i want to delete every empty row.

this is my code :

Excel::selectSheetsByIndex(0)->load($path, function($reader){
       $results = $reader->noHeading()->toArray();
         foreach ($results as $row) {
            //my code
         }
       }, 'UTF-8');

So please if someone has any idea how i can do that i will be very appreciative

Answer

Crowlex picture Crowlex · Apr 4, 2017

I think you can do it in this way

/** @var LaravelExcelReader $data */
        $data = \Excel::load('file.xls', function ($reader) {
            $reader->limitRows(20);
            $reader->ignoreEmpty();
        })->get()->toArray();

        # remove empty rows
        $data = array_filter($data);