2017 © Pedro Peláez
 

library smart-csv

Easy CSV manipulation.

image

colbygatte/smart-csv

Easy CSV manipulation.

  • Friday, September 29, 2017
  • by colbygatte
  • Repository
  • 1 Watchers
  • 0 Stars
  • 104 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 22 Versions
  • 0 % Grown

The README.md

Smart Csv

File: people.csv

name,age,address,Favorite Type Of Food,attribute 1,value 1,note 1,attribute 2,value 2,note 2
Colby,26,1234 Country Lane,burgers,hair color,brown,nil,height,6,so tall
Mark,28,4321 City Street,ramen,shoe size,9,big foot!,height,5'9'',so short
Tammy,29,2314 Bayou Road,steak,pant size,4,nil,ring size,7,beautiful!

Overview of helper functions

  • csv() - Used for creating CSV's. Accepts one parameter: The header.
  • csv_sip() - Read a CSV row-by-row. Accepts on parameter: The file path or file handle
  • csv_slurp() - Read an entire CSV into memory. Accepts one parameter: The file path or file handle
  • csv_writer()
  • csv_alter()

Basic usage

Use csv() for creating a new CSV, (*1)

<?php
$csv = csv(['name', 'age']);
$csv->append(['Mark', 28], ['Colby', 26])  // Multiple rows can be passed at once
    ->write('info.csv');

csv_sip() will read a CSV file row-by-row (not line by line - a single row of a CSV can span multiple lines), (*2)

<?php
foreach (csv_sip('people.csv') as $row) {
    echo $row->name; 
}

csv_slurp() will read the entire CSV file at once, (*3)

<?php
$csv = csv_slurp('people.csv');
foreach ($csv as $row) {
    echo $row->age;
}

csv_writer() is for writing directly to a CSV file., (*4)

<?php
$writer = csv_writer('data.csv', ['time', 'cost']);
$writer->append(['4pm', '$100'], ['1am', '$14']); // Append automatically writes each row

csv_alter() uses Sip to iterate over each column, and after the iteration, the column is written to a new csv. This allows for easy data manipulation without changing the source., (*5)

<?php
$alter = csv_alter('people.csv', 'altered-people.csv');
foreach ($alter as $row) {
    // Change stuff
    $row->name = strtoupper($row->name);
    // If the delete() method is called on the row, it will not be included in the new CSV
    $row->delete();
}

Column Grouping

Column grouping works by matching header endings. Example, attribute 1 and value 1 are matched because they both end with the same thing, which is ' 1'. It is not matching the numbers, but the entire remaining string after attribute and value., (*6)

<?php
$csv = csv_sip('people.csv')->makeGroup('attributes', 'attribute', ['value', 'note']);
foreach ($csv as $row) {
    print_r($row->groups()->attributes);
}

Output (for first value):, (*7)

Array
(
    [0] => Array
        (
            [attribute] => hair color
            [value] => brown
            [note] => nil
        )

    [1] => Array
        (
            [attribute] => height
            [value] => 6
            [note] => so tall
        )

)

The Versions

29/09 2017

dev-master

9999999-dev

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

05/09 2017

v0.0.10

0.0.10.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

05/09 2017

v0.0.9

0.0.9.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

05/09 2017

v0.0.8

0.0.8.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

12/08 2017

v0.0.7

0.0.7.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

12/08 2017

v0.0.6

0.0.6.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

11/08 2017

v0.0.5

0.0.5.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

11/08 2017

v0.0.4

0.0.4.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

05/08 2017

v0.0.3

0.0.3.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

04/08 2017

v0.0.2

0.0.2.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

04/08 2017

v0.0.1

0.0.1.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

21/07 2017

dev-rework-option-parser

dev-rework-option-parser

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

15/06 2017

1.3.1-stable

1.3.1.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

10/06 2017

1.3.0-stable

1.3.0.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

08/06 2017

1.2.0-stable

1.2.0.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

08/06 2017

1.2.1

1.2.1.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

04/06 2017

1.1.0-stable

1.1.0.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

04/06 2017

1.1.0-BETA

1.1.0.0-beta

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

03/06 2017

1.0.1.x-dev

1.0.1.9999999-dev

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

03/06 2017

1.0.1-stable

1.0.1.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

03/06 2017

1.x-dev

1.9999999.9999999.9999999-dev

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte

03/06 2017

1.0.0

1.0.0.0

Easy CSV manipulation.

  Sources   Download

MIT

The Development Requires

by Avatar colbygatte