kedrigern/data-table

Manipulation with table organized collection of data (csv, excel). Contains functions for work with whole columns, rows, filtering etc.

v1.0 2014-09-05 21:22 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:36:19 UTC


README

#Data table

For robust manipulation with table organized collection of data (excel, csv tables).

Provide ability to work with whole rows and columns, call callbacks, resort, rename, filter. Include few predefined callbacks. See examples.

Import csv and array. Export to plain array, text, html, csv. Export is suitable for DB frameworks (Nette\Database).

Build Status

##Install

The best way how to install is use Composer:

php composer.phar require kedrigern/data-table

Run tests: vendor/bin/tester -c test/ test/

##Examples

Suppose:

$table = new \Kedrigern\DataTable\RecordTable();
$table->loadFromArray(array(
	array("Name", "Surname", "Age"),
	array("Jane", "Roe", 29),
	array("John", "Doe", 30)
));
$table->useFirstRowAsHeader();

// Sum the ages = 59
$ageSum = $table->colSum("Age");

$table->sortByCol("Surname");
$table->callToCol("Surname", 'strtoupper');

$newCol = range(1,$table->getRowsNum());
$table->appendCol($newCol, "Unique");

$newTable = $table->resortColsByNewHeader(array("Unique", "Surname", "Name"));

$newTable contains:

1 DOE John
2 ROE Jane

CSV with header

Very common issue when you have some data in csv with header and you need only subset:

$table->loadFromCsvFile(__DIR__ . '/../data/data4.csv');
$table->useFirstRowAsHeader();

$map = array(
	"Alpha" => "A",
	"Beta" => "B",
);

$table2 = $table->renameColumns($map);

Where table2 contains column Alpha renamed to A and column Beta renamed to B. See test.

Remove if

Remove all rows with even number in first column:

$isEven = function($row) {
  return ($row[0] % 2) == 0;
};

$removed = $t->removeRowsIf($isEven);

In removed are number of modified rows.

Parse datetime

$table->loadFromArray(array(
	array("Name", "Surname", "Age", "Born", "Registered"),
	array("Jane", "Roe", 29, "1990-1-1", "2013-12-30 01:02:03"),
	array("John", "Doe", 30, "1990-1-1", "2014-12-30 01:02:03")
));
$table->useFirstRowAsHeader();

$func = array('\Kedrigern\DataTable\Callback','toDatetime');

$table->callToCol(3, $func, array('Y-m-d', 'M y'));
$table->callToCol(4, $func, array('Y-m-d H:i:s', 'U', 'Europe/London'));

Now Born seems: ["Jan 90", "Feb 91"], and registered: ["1388361723", "1419897723"]

Join and split columns

$table->loadFromArray(array(
	array("Name", "Surname"),
	array("Jane", "Roe"),
	array("John", "Doe")
));

$table->joinCols(["Name", "Surname"], "Fullname");

In Fullname column you get: ["Jane Roe", "John Doe"].

Or opposite way:

$table->loadFromArray(array(
	array("Fullname"),
	array("Jane Roe"),
	array("John Doe")
));

$table->splitCol("Fullname", ["Name", "Surname"]);

Callbacks

Predefined callbacks:

  • join
  • joinWith
  • split
  • toDatetime
  • isEven
  • isOdd
  • allowValues
  • disallowValues
  • unicodeTrim

##Author and contact