Giter Site home page Giter Site logo

php-csv's Introduction

php-csv

Simple csv reader writer

Minimal csv reader with some useful functions

Install


git clone https://github.com/nickolanack/php-csv.git
cd php-csv
composer install


Usage

	include_once __DIR__ . '/vendor/autoload.php';


	//create a new csv and get string output
   	$csvString = (new nblackwe\Csv())->create(array( 'label-one',  'label-two',  'label-three'))
    	->addRow(array('1','2','3'))
    	->addRow(array('4','5','6'))
   		->addRows(array(
	   		array( '7', '8','9'),
	   		array( '1', '2','3'),
	   		array( '4', '5','6')
	    ))
	    ->iterateRowsAssoc(function($value, $index){

	    	echo $value['label-one'];

	    })
	    ->iterateRowsObject(function($value, $index){

	    	echo $value->{'label-one'};

	    })
	    ->iterateRows(function($value, $index){

	    	echo $value[0];

	    })
	    ->toString();


	//read from a string
	$csv=(new nblackwe\Csv())->fromString($csvString); 


	//write to a file
	$csv->toFile('/tmp/test.csv');


	//read from a file
	(new nblackwe\Csv())->fromFile('/tmp/test.csv')
		->countRows(); // should be 5 (not counting header)




	//distinct values in a column
	$csv->distinctValues('label-one'); // should be array('1', '4', '7');
	
	$csv->getFieldNames(); //returns array('label-one',  'label-two',  'label-three')

	

	//other functions


	
	$csv->getRowsWithValue(array('label-one'=>'1', 'label-three'=>'3')); // returns array(0,3)
    $csv->getRowsWithValue(array('label-three'=>'9')); // returns array(2)


    //read field names without parsing document
    (new nblackwe\Csv())->getFieldNames('/tmp/test.csv'); //returns array('label-one',  'label-two',  'label-three')
    	


	//read from a resource can be used to change character encoding etc

	ini_set('auto_detect_line_endings', true);

    $handle = fopen('/tmp/test.csv', 'r');

    if (!$handle) {
        throw new Exception('Invalid File, or Failed to read');
    }
    $csv=(new nblackwe\Csv())->fromResource($handle);
    fclose($handle);

php-csv's People

Contributors

nickolanack avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.