PHP code example of jabranr / csv-parser

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

    

jabranr / csv-parser example snippets


$csv = new Jabran\CSV_Parser();

/* @param: string $str */
$csv->fromString($str);

/* @param: resource $resource (f.e. resource created using fopen()) */
$csv->fromResource($resource);

/* @param: string $path */
$csv->fromPath($path);

/**
 * Set $headers true/false to y
 *
 * @param: boolean $headers (Default: true)
 * @return: array
 */
$csv->parse($headers);

/**
 * Set columns
 * @param array $columns
 * @return Jabran\CSV_Parser
 */
$csv->setColumns($columns);

/**
 * Set rows
 * @param array $rows
 * @return Jabran\CSV_Parser
 */
$csv->setRows($rows);

/**
 * Set headers
 * @param array $headers
 * @return Jabran\CSV_Parser
 */
$csv->setHeaders($headers);

/**
 * Get columns
 * @return array
 */
$csv->getColumns();

/**
 * Get rows
 * @return array
 */
$csv->getRows();

/**
 * Get headers
 * @return array
 */
$csv->getHeaders();



$csv = new Jabran\CSV_Parser;

$str = 'id,first_name,last_name;1,Jabran,Rafique';

$csv->fromString($str);

// Output with headers:
$csv->parse();

Array(
  [id] => 1,
  [first_name] => 'Jabran',
  [last_name] => 'Rafique'
)

// Output without headers:
$csv->parse(false);

Array(
  [0] => array(
    [0] => 'id',
    [1] => 'first_name',
    [2] => 'last_name'
 ),
  [1] => array(
    [0] => 1,
    [1] => 'Jabran',
    [2] => 'Rafique'
 )
)