GithubHelp home page GithubHelp logo

smrtr-datagrid's Introduction

Smrtr DataGrid Build Status Codeship Status for joegreen88/smrtr-datagrid Coverage Status

A tool for working with 2d arrays, spreadsheets and data grids in PHP.

Examples

use Smrtr\DataGrid;

$grid = new DataGrid(
  array(
      array("First name", "Last Name", "Position", "Age"),
      "WR" => array("Wayne", "Rooney", "striker", 27),
      "KG" => array("Kieran", "Gibbs", "left back", 23),
      "GB" => array("Gareth", "Barry", "midfielder", 32),
      "TW" => array("Theo", "Walcott", "striker", 24)
  ),
  DataGrid::ASSOC_COLUMN_FIRST,
  DataGrid::ASSOC_ROW_KEYS
);

$grid->saveCSV('/path/to/file.csv');

$grid->serveJSON('download.json');

$grid2 = new DataGrid;
$grid2->loadCSV('/path/to/file.csv', true, true);

print_r(
  $grid
    ->searchRows('Age<25 + Position=striker')
    ->getRowLabels()
); // [ "TW" ]

print_r(
  $grid
    ->searchRows('Age<25 - Position=striker')
    ->getRowLabels()
); // [ "KG" ]

print_r(
  $grid
    ->searchRows('Age<30 + (Position="left back" , Position=midfielder)')
    ->getRowLabels()
); // [ "KG" ]

print_r(
  $grid
    ->searchRows('(Age<30 + Position="left back") , Position=midfielder')
    ->getRowLabels()
); // [ "KG", "GB" ]

echo $grid->row('WR')['First name']; // "Wayne"

$grid->renameColumn('First name', 'Forename');

$s = serialize($grid);

$grid = unserialize($s);

$inverse = $grid->transpose();

echo ($grid->getRowKeys() === $inverse->getColumnKeys()) ? 'Y' : 'N'; // "Y"

Install with composer

{
    "require": {
        "smrtr/datagrid": ">=1.3.2"
    }
}

composer update

Tutorial

Click here.

smrtr-datagrid's People

Contributors

joegreen88 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

smrtr

smrtr-datagrid's Issues

Query function

Implement two functions, queryRows() and queryColumns(), which search the DataGrid for rows and columns respectively which match the provided key(s).

e.g. $Grid->queryRows(array('first_name'=>'Joseph'))

Formulas

Suggest a new class, Smrtr\DataGridFormula. Let objects of this type be assigned to cells as values. Formula objects have a method evaluate which is triggerred by the DataGrid when getting values including that cell, and passes in the X and Y position of the cell being evaluated as well as a reference to the grid for looking up other cell values. The Formula looks up the values, evaluates the result, and passes the result back as the return value. The DataGrid then replaces the formula object with the value in the returned data, without removing the formula from $grid->data.

Some helper methods on the Grid object would be useful too:

  • flatten() which evaluates all formulas and replaces the formulas with the calculated values.
  • info('formulas') which gets a count of formulas on the grid (this info can be cached on the instance)

there is more that can be done, this is just the groundwork.

Yaml

Yaml parsing and writing

Searching on the grid without labels

Currently if I want to use the searchRows() method on a grid with no column labels then i am in a bit of a bind.

Suggest using some notation to access keys just like we access labels. One such mapping could be /n where n is the key we want to search on.

Example:

$grid->searchRows('/0*=a + /2<50');

i.e. find rows where the first column contains a value containing the 'a' character and the third column contains a value less than 50.

Get filter rules to match behaviour of each row

Here we can call each row by key

$grid->eachRow(function($key, $row){ $row['slug'] = trim($row['slug'],'/'); },true);

Here however, we need to know the actual index

$grid3 = $grid->filterRows(function($key, $label, $row){ // keep top level pages return !stristr($row[0],'/'); });

Save method

Method called save which saves the grid to a file. Writes a serialized instance by default and can also proxy to saveCsv and saveJson via a parameter.

Pivot tables

Need to explore this idea in alot more detail and prototype some ideas at home.

deleteEmptyRows should look for empty strings too

        // filter out empty rows
        $grid = $grid->filterRows(function($key, $label, $row) {
            return array_reduce($row, function(&$keep, $value){
                $keep = '' === (string) $value ? $keep : true;
                return $keep;
            }, false);
        });

hasValue($rowOrColumn)

hasValue( $value )

hasValue( $value, $rowOrColumn, $keyOrLabel )

rowHasValue( $value, $rowKeyOrLabel )

columnHasValue( $value, $columnKeyOrLabel )

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.