GithubHelp home page GithubHelp logo

class-csv2hash's Introduction

PHP CLASS CSV2HASH

Description

If you have csv file with column names in the first row ...

This class reads the csv and returns each row using the column names as key. If the column name contains a dot "." it is used to handle subkeys.

Example

My example csv data:

id,hide,label,flag.x,flag.y
1,1,one,0,1
2,0,two,1,0
3,0,three,1,0

... can be be returned as

Array
(
    [0] => Array
        (
            [id] => 1
            [hide] => 1
            [label] => one
            [flag] => Array
                (
                    [x] => 0
                    [y] => 1
                )

        )

    [1] => Array
        (
            [id] => 2
            [hide] => 0
            [label] => two
            [flag] => Array
                (
                    [x] => 1
                    [y] => 0
                )

        )

    [2] => Array
        (
            [id] => 3
            [hide] => 0
            [label] => three
            [flag] => Array
                (
                    [x] => 1
                    [y] => 0
                )

        )

)

Installation

Copy the file ./src/csv2hash.class.php to a good place.

Usage

Read CSV and get all data

require_once(dirname(__DIR__).'/src/csv2hash.class.php');
$sCsvfile='/full/path/of/file.csv';

$oC2H=new csv2hash();
$oC2H->setCsvFile($sCsvfile);

print_r($oC2H->getData(),1)

Set another delimiter

Default delimiter for csv data is a comma.

$oC2H->setDelimiter(';');
$oC2H->setCsvFile($sCsvfile);

echo '<pre>'.print_r($oC2H->getData(), 1).'</pre>';

or

$oC2H->setCsvFile($sCsvfile, ';');
echo '<pre>'.print_r($oC2H->getData(), 1).'</pre>';

Generate subkeys in data

If a header has values divided by a given string you can create subkeys in the data.

Example:

In the demo csv are colums flag.x and flag.y. Set the dot as splitting string.

$oC2H->setHeaderSplitter('.');
$oC2H->setCsvFile($sCsvfile);

echo '<pre>'.print_r($oC2H->getData(), 1).'</pre>';

BTW: This is the source for the array in the beginning of the readme.

Get filtered data

At first: This is a very basic implementation. But maybe it helps you. If not you can loop over all result entries and filter yourselfes. Or add it in the class to send a merge request.

The method getData supports 2 parameters

  • Array of arrays with filter definitions
    • each filter item has the structure
      • name of column in CSV to filter
      • array([Compare], [Value]) Compare is one of
        • "is" - value in CSV is exactly given value (bool|int|string)
        • "not" - value in CVS does not match (bool|int|string)
        • "matches" - vanlue in CSV matches a given regex
  • Method if you have more than one filters: how to combine them; one of "and" | "or"

Here is a single filter with a regex to match the column "label"

// filter by column "label" to return lines that match 
// regex /^o/ (start with letter "o" --> matches the line with label "one")
$aFilter=array(
	'label'=>array('matches', '/^o/'),
);
echo '<pre>'.print_r($oC2H->getData($aFilter), 1).'</pre>';

2nd Example:

2 filters using OR function - getData($aFilter, 'or'). It matches the first row with hide=1 and the 3rd line by label "three".

$aFilter=array(
	'hide'=>array('is', "1"),
	'label'=>array('matches', '/^th/'),
);
echo '<pre>'.print_r($oC2H->getData($aFilter, 'or'), 1).'</pre>';

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.