albinvar/php-csv-generator

The Termux Webzone CLI App.

v2.1.0 2021-06-06 21:24 UTC

README

badge.svg?branch=main 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c62696e7661722f7068702d6373762d67656e657261746f723f6c6162656c3d76657273696f6e 68747470733a2f2f706f7365722e707567782e6f72672f616c62696e7661722f7068702d6373762d67656e657261746f722f646f776e6c6f616473 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f616c62696e7661722f7068702d6373762d67656e657261746f72 68747470733a2f2f696d672e736869656c64732e696f2f61706d2f6c2f476974687562

Table of Contents

Introduction

A simple php library used to generate php array to csv, json and csv to php array and json.

Installation

composer require albinvar/php-csv-generator

Attach the library to your php code.

<?php

use PhpCsv\Generator;

require_once("./vendor/autoload.php");

$obj = new Generator();

Manual installation

  • Download the script from here.
  • Add the script to your php code.
<?php

require_once "Generator.php";

Updation

composer update albinvar/php-csv-generator

Features

  • Convert CSV to php array
  • Convert array to CSV format
  • Convert array & CSV to json
  • Import JSON and Convert to array.
  • Export JSON format and save or stream json file
  • Export CSV file & save to preferred location
  • Export CSV file & Stream to browser

Array to CSV

You can convert an array into a csv file using the following example.

<?php

use PhpCsv\Generator;

require_once("./vendor/autoload.php");

$columns = ['Name', 'Age'];
$array = [ 
	['John', 28],
	['Johana', 23],
	['Adam', 32],
];

$object = new Generator();
$object->setArray($array, $columns);
$object->makeCsv();
$object->getCsv(); //(Optional) Get CSV as a string.
$object->exportCsv('data.csv', true);

$object->exportCsv('data.csv', true); The first argument excepts the filenams and 2nd argument excepts the download type which expects a boolean format.

CSV to Array

You can convert a csv file to an array using the following example.

<?php

use PhpCsv\Generator;

require_once("./vendor/autoload.php");

$object = new Generator();
$object->importCsv('data.csv');
$array = $object->getArray();

var_dump($array);

JSON to Array

You can convert a JSON file to an array using the following example.

<?php

use PhpCsv\Generator;

require_once("./vendor/autoload.php");

$object = new Generator();
$object->importJson('data.json');
$array = $object->getArray();

var_dump($array);

Export to JSON format

You can export to JSON from both CSV or Array The first argument expects the filename and 2nd argument excpects the download type which should be in a boolean format.

// returns json string.
echo $object->exportJson();

// creates json file and download to browser.
$object->exportJson('data.json', true);

// creates json file and saves it to specific location.
$object->exportJson('data.json', false);

Contributing

Pull requests are always welcome...

License

MIT. See LICENSE for more details.