PHP code example of unicframework / csv-parser

1. Go to this page and download the library: Download unicframework/csv-parser 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/ */

    

unicframework / csv-parser example snippets


use CSVParser\CSV;

$csv = new CSV();

//Parse data from csv file
$csv->parse('data.csv');

//Parse array data
$csv->parse($arrayData);

//Parse object data
$csv->parse($jsonData);

//Parse json data
$csv->parse($objectData);

//Get header
$header = $csv->getHeader();

//Get parsed data to array format
$data = $csv->toArray();

//Select data from parsed data
$data = $csv->toArray(['Name', 'Email']);

//Get parsed data to object format
$data = $csv->toObject();

//Select data from parsed data
$data = $csv->toObject(['Name', 'Email']);

//Get parsed data to json format
$data = $csv->toJson();

//Select data from parsed data
$data = $csv->toJson(['Name', 'Email']);

//Get parsed data to csv format
$data = $csv->toCsv();

//Select data from parsed data
$data = $csv->toCsv(['Name', 'Email']);

//Get row count
$rows = $csv->rowCount();

//Get header count
$cols = $csv->headerCount();

//Select 10 records
$csv->limit(10);
$data = $csv->toArray();

//Select from 5 to 10 records
$csv->limit(5, 10);
$data = $csv->toArray();

//Ignore header from csv file
$csv->ignoreHeader(true);

//Ignore csv header cse
$csv->ignoreHeaderCase(true);

//Ignore csv enclosure
$csv->ignoreEnclosure(true);

//Set header offset of csv file
$csv->headerOffset(0);

//Set custom header to csv file
$csv->setHeader(['Name', 'Email']);

//Set delimiter
$csv->setDelimiter('|');

//Set enclosure
$csv->setEnclosure('"');

//Set escape character
$csv->setEscape('//');

//Parse csv file data
$csv->parse('data.csv');

//Get data from parsed data
$data = $csv->toArray();


//Parse csv file data
$csv->parse('data.csv');

//Get total sum of given field
$total_price = $csv->sum('price');

//Get minimum from given field
$min_price = $csv->min('price');

//Get maximum from given field
$max_price = $csv->max('price');

//Get average of given field
$average_price = $csv->average('price');