PHP code example of mahmud / sheet

1. Go to this page and download the library: Download mahmud/sheet library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mahmud / sheet example snippets


use Mahmud\Sheet\SheetReader;

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")                        // Optional: You can set delimiter for CSV file
            ->ignoreRow(0)                          // Optional: Skip the header row
            ->columns(['id', 'name', 'age'])        // Arbitary column name that will be mapped sequentially for each row
            ->onEachRow(function($row, $index){
                // This callback will be executed for each row
                var_dump($row);     // Current row in associative array
                var_dump($index);   // Current index of the row
            })->read();

use Mahmud\Sheet\SheetReader;

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware(function($row, $index){
                $row['age'] = $row['age'] . " Years";
                
                return $row;
            })
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();


class AgeMiddleware{
    public function handle($row, $index) {
        $row['age'] = $row['age'] . " Years";
    
        return $row;
    }
}

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware(new AgeMiddleware)
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();


SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware([
                new AgeMiddleware,
                new AnotherMiddleware,
            ])
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware(function($row){
                if($row['id'] == 1){
                    return null;
                }
                
                return $row;
            })
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();

$total = SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
                        ->totalRows();
                        
var_dump($total);       // 4