File_CSV_DataSource
[ class tree: File_CSV_DataSource ] [ index: File_CSV_DataSource ] [ all elements ]

Class: File_CSV_DataSource

Source Location: /File_CSV_DataSource-1.0.1/DataSource.php

Class Overview


csv data fetcher


Author(s):

Copyright:

  • 2008 Kazuyoshi Tlacaelel

Methods


Inherited Variables

Inherited Methods


Class Details

[line 59]
csv data fetcher

Sample snippets refer to this csv file for demonstration.

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa



[ Top ]


Method Detail

__construct (Constructor)   [line 111]

void __construct( [mixed $filename = null])

data load initialize

Parameters:

mixed   $filename   —  please look at the load() method

[ Top ]

appendColumn   [line 640]

boolean appendColumn( string $column, [mixed $values = null])

column appender

Appends a column and each or all values in it can be dinamically filled. Only when the $values argument is given.

  1.   var_export($csv->fillColumn('age'99));
  2.   true
  3.  
  4.   var_export($csv->appendColumn('candy_ownership'array(994465)));
  5.   true
  6.  
  7.   var_export($csv->appendColumn('import_id'111111111));
  8.   true
  9.  
  10.   var_export($csv->connect());
  11.  
  12.   array (
  13.     0 =>
  14.     array (
  15.       'name' => 'john',
  16.       'age' => 99,
  17.       'skill' => 'knows magic',
  18.       'candy_ownership' => 99,
  19.       'import_id' => 111111111,
  20.     ),
  21.     1 =>
  22.     array (
  23.       'name' => 'tanaka',
  24.       'age' => 99,
  25.       'skill' => 'makes sushi',
  26.       'candy_ownership' => 44,
  27.       'import_id' => 111111111,
  28.     ),
  29.     2 =>
  30.     array (
  31.       'name' => 'jose',
  32.       'age' => 99,
  33.       'skill' => 'dances salsa',
  34.       'candy_ownership' => 65,
  35.       'import_id' => 111111111,
  36.     ),
  37.   )


Parameters:

string   $column   —  an item returned by getHeaders()
mixed   $values   —  same as fillColumn()

[ Top ]

appendRow   [line 1457]

boolean appendRow( array $values)

row appender

Aggregates one more row to the currently loaded dataset

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

first let's load the file and output whatever was retrived.

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');
  4.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

now lets do some modifications, let's try adding three rows.

  1.   var_export($csv->appendRow(1));
  2.   var_export($csv->appendRow('2'));
  3.   var_export($csv->appendRow(array(333)));

output

  1.   true
  2.   true
  3.   true

and now let's try to see what has changed

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.     3 =>
  21.     array (
  22.       'name' => 1,
  23.       'age' => 1,
  24.       'skill' => 1,
  25.     ),
  26.     4 =>
  27.     array (
  28.       'name' => '2',
  29.       'age' => '2',
  30.       'skill' => '2',
  31.     ),
  32.     5 =>
  33.     array (
  34.       'name' => 3,
  35.       'age' => 3,
  36.       'skill' => 3,
  37.     ),
  38.   )

  • Access: public

Parameters:

array   $values   —  the values to be appended to the row

[ Top ]

connect   [line 316]

array connect( [array $columns = array()])

header and row relationship builder

Attempts to create a relationship for every single cell that was captured and its corresponding header. The sample below shows how a connection/relationship is built.

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

php implementation

  1.   $csv = new File_CSV_DataSource;
  2.   $csv->load('my_cool.csv');
  3.  
  4.   if (!$csv->isSymmetric()) {
  5.       die('file has headers and rows with different lengths
  6.       cannot connect');
  7.   }
  8.  
  9.   var_export($csv->connect());
  10.  
  11.   array (
  12.     0 =>
  13.     array (
  14.       'name' => 'john',
  15.       'age' => '13',
  16.       'skill' => 'knows magic',
  17.     ),
  18.     1 =>
  19.     array (
  20.       'name' => 'tanaka',
  21.       'age' => '8',
  22.       'skill' => 'makes sushi',
  23.     ),
  24.     2 =>
  25.     array (
  26.       'name' => 'jose',
  27.       'age' => '5',
  28.       'skill' => 'dances salsa',
  29.     ),
  30.   )

You can pass a collection of headers in an array to build a connection for those columns only!

  1.   var_export($csv->connect(array('age')));
  2.  
  3.   array (
  4.     0 =>
  5.     array (
  6.       'age' => '13',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'age' => '8',
  11.     ),
  12.     2 =>
  13.     array (
  14.       'age' => '5',
  15.     ),
  16.   )


Parameters:

array   $columns   —  the columns to connect, if nothing is given all headers will be used to create a connection

[ Top ]

countHeaders   [line 225]

integer countHeaders( )

header counter

retrives the total number of loaded headers

  • Return: gets the length of headers
  • Access: public

[ Top ]

countRows   [line 1332]

integer countRows( )

row counter

This function will exclude the headers

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

php implementation

  1.    $csv = new File_CSV_DataSource;
  2.    $csv->load('my_cool.csv');
  3.    var_export($csv->countRows())// returns 3

  • Access: public

[ Top ]

createHeaders   [line 1965]

boolean createHeaders( string $prefix)

header creator

uses prefix and creates a header for each column suffixed by a numeric value

by default the first row is interpreted as headers but if we have a csv file with data only and no headers it becomes really annoying to work with the current loaded data.

this function will create a set dinamically generated headers and make the current headers accessable with the row handling functions

Note: that the csv file contains only data but no headers sample of a csv file "my_cool.csv"

  1.    john,13,knows magic
  2.    tanaka,8,makes sushi
  3.    jose,5,dances salsa

checks if the csv file was loaded

  1.    $csv = new File_CSV_DataSource;
  2.    if (!$csv->load('my_cool.csv')) {
  3.       die('can not load csv file');
  4.    }

dump current headers

  1.    var_export($csv->getHeaders());

standard output

  1.    array (
  2.      0 => 'john',
  3.      1 => '13',
  4.      2 => 'knows magic',
  5.    )

generate headers named 'column' suffixed by a number and interpret the previous headers as rows.

  1.    $csv->createHeaders('column')

dump current headers

  1.    var_export($csv->getHeaders());

standard output

  1.    array (
  2.      0 => 'column_1',
  3.      1 => 'column_2',
  4.      2 => 'column_3',
  5.    )

build a relationship and dump it

  1.    var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'column_1' => 'john',
  5.       'column_2' => '13',
  6.       'column_3' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'column_1' => 'tanaka',
  11.       'column_2' => '8',
  12.       'column_3' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'column_1' => 'jose',
  17.       'column_2' => '5',
  18.       'column_3' => 'dances salsa',
  19.     ),
  20.   )


Parameters:

string   $prefix   —  string to use as prefix for each independent header

[ Top ]

fillCell   [line 1043]

boolean fillCell( integer $x, integer $y, mixed $value)

cell value filler

replaces the value of a specific cell

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

php implementation

  1.    $csv = new File_CSV_DataSource;
  2.  
  3.    // load the csv file
  4.    $csv->load('my_cool.csv');
  5.  
  6.    // find out if the given coordinate is valid
  7.    if($csv->hasCell(11)) {
  8.  
  9.        // if so grab that cell and dump it
  10.        var_export($csv->getCell(11));       // '8'
  11.  
  12.        // replace the value of that cell
  13.        $csv->fillCell(11'new value');  // true
  14.  
  15.        // output the new value of the cell
  16.        var_export($csv->getCell(11));       // 'new value'
  17.  
  18.    }

now lets try to grab the whole row

  1.    // show the whole row
  2.    var_export($csv->getRow(1));

standard output

  1.    array (
  2.      0 => 'tanaka',
  3.      1 => 'new value',
  4.      2 => 'makes sushi',
  5.    )


Parameters:

integer   $x   —  the row to fetch
integer   $y   —  the column to fetch
mixed   $value   —  the value to fill the cell with

[ Top ]

fillColumn   [line 742]

void fillColumn( mixed $column, [mixed $values = null])

collumn data injector

fills alll the data in the given column with $values

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

php implementation

  1.    $csv = new File_CSV_DataSource;
  2.    $csv->load('my_cool.csv');
  3.  
  4.    // if the csv file loads
  5.    if ($csv->load('my_cool.csv')) {
  6.  
  7.       // grab all data within the age column
  8.       var_export($csv->getColumn('age'));
  9.  
  10.       // rename all values in it with the number 99
  11.       var_export($csv->fillColumn('age'99));
  12.  
  13.       // grab all data within the age column
  14.       var_export($csv->getColumn('age'));
  15.  
  16.       // rename each value in a column independently
  17.       $data = array(123);
  18.       $csv->fillColumn('age'$data);
  19.  
  20.       var_export($csv->getColumn('age'));
  21.    }

standard output

  1.    array (
  2.      0 => '13',
  3.      1 => '8',
  4.      2 => '5',
  5.    )

  1.    true

  1.    array (
  2.      0 => 99,
  3.      1 => 99,
  4.      2 => 99,
  5.    )

  1.    array (
  2.      0 => 1,
  3.      1 => 2,
  4.      2 => 3,
  5.    )

  • Access: public

Parameters:

mixed   $column   —  the column identified by a string
mixed   $values   —  ither one of the following
  • (Number) will fill the whole column with the value of number
  • (String) will fill the whole column with the value of string
  • (Array) will fill the while column with the values of array the array gets ignored if it does not match the length of rows

[ Top ]

fillRow   [line 1584]

boolean fillRow( integer $row, mixed $values)

fillRow

Replaces the contents of cells in one given row with $values.

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

if we load the csv file and fill the second row with new data?

  1.   // load the library
  2.   require_once 'File/CSV/DataSource.php';
  3.   $csv = new File_CSV_DataSource;
  4.  
  5.   // load csv file
  6.   $csv->load('my_cool.csv');
  7.  
  8.   // fill exitent row
  9.   var_export($csv->fillRow(1'x'));

output

  1.   true

now let's dump whatever we have changed

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'x',
  11.       'age' => 'x',
  12.       'skill' => 'x',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

now lets try to fill the row with specific data for each cell

  1.   var_export($csv->fillRow(1array(123)));

output

  1.   true

and dump the results

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 1,
  11.       'age' => 2,
  12.       'skill' => 3,
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )


Parameters:

integer   $row   —  the row to fill identified by its key
mixed   $values   —  the value to use, if a string or number is given the whole row will be replaced with this value. if an array is given instead the values will be used to fill the row. Only when the currently loaded dataset is symmetric

[ Top ]

flush   [line 2257]

void flush( )

object data flusher

tells this object to forget all data loaded and start from scratch

  • Access: protected

[ Top ]

getAsymmetricRows   [line 412]

array getAsymmetricRows( )

asymmetric data fetcher

finds the rows that do not match the headers length

lets assume that we add one more row to our csv file. that has only two values. Something like

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa
  5.    niki,6

Then in our php code

  1.    $csv->load('my_cool.csv');

The result

  1.    array (
  2.      0 =>
  3.      array (
  4.        0 => 'niki',
  5.        1 => '6',
  6.      ),
  7.    )


[ Top ]

getCell   [line 971]

mixed|false getCell( integer $x, integer $y)

cell fetcher

gets the value of a specific cell by given coordinates

Note: That indexes start with zero, and headers are not searched!

For example if we are trying to grab the cell that is in the second row and the third column

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

we would do something like

  1.    var_export($csv->getCell(12));

and get the following results

  1.    'makes sushi'


Parameters:

integer   $x   —  the row to fetch
integer   $y   —  the column to fetch

[ Top ]

getColumn   [line 529]

array getColumn( string $name)

column fetcher

gets all the data for a specific column identified by $name

Note $name is the same as the items returned by getHeaders()

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

php implementation

  1.    $csv = new File_CSV_DataSource;
  2.    $csv->load('my_cool.csv');
  3.    var_export($csv->getColumn('name'));

the above example outputs something like

  1.    array (
  2.      0 => 'john',
  3.      1 => 'tanaka',
  4.      2 => 'jose',
  5.    )


Parameters:

string   $name   —  the name of the column to fetch

[ Top ]

getHeaders   [line 212]

array getHeaders( )

header fetcher

gets csv headers into an array

  1.    var_export($csv->getHeaders());
  2.  
  3.    array (
  4.      0 => 'name',
  5.      1 => 'age',
  6.      2 => 'skill',
  7.    )

  • Access: public

[ Top ]

getRawArray   [line 1845]

array getRawArray( )

raw data as array

Gets the data that was retrived from the csv file as an array

Note: that changes and alterations made to rows, columns and values will also reflect on what this function retrives.


[ Top ]

getRow   [line 1174]

array getRow( integer $number)

row fetcher

Note: first row is zero

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

load the library and csv file

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');

lets dump currently loaded data

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

Now let's fetch the second row

  1.   var_export($csv->getRow(1));

output

  1.   array (
  2.     0 => 'tanaka',
  3.     1 => '8',
  4.     2 => 'makes sushi',
  5.   )

  • Return: the row identified by number, if $number does not exist an empty array is returned instead
  • Access: public

Parameters:

integer   $number   —  the row number to fetch

[ Top ]

getRows   [line 1288]

array getRows( [array $range = array()])

multiple row fetcher

Extracts a rows in the following fashion

  • all rows if no $range argument is given
  • a range of rows identified by their key
  • if rows in range are not found nothing is retrived instead
  • if no rows were found an empty array is returned
sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

load the library and csv file

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');

lets dump currently loaded data

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

now get the second and thirdh row

  1.   var_export($csv->getRows(array(12)));

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       0 => 'tanaka',
  5.       1 => '8',
  6.       2 => 'makes sushi',
  7.     ),
  8.     1 =>
  9.     array (
  10.       0 => 'jose',
  11.       1 => '5',
  12.       2 => 'dances salsa',
  13.     ),
  14.   )

now lets try something odd and the goodie third row

  1.   var_export($csv->getRows(array(92)));

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       0 => 'jose',
  5.       1 => '5',
  6.       2 => 'dances salsa',
  7.     ),
  8.   )

  • Access: public

Parameters:

array   $range   —  a list of rows to retrive

[ Top ]

hasCell   [line 1093]

void hasCell( mixed $x, mixed $y)

checks if a coordinate is valid

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

load the csv file

  1.    $csv = new File_CSV_DataSource;
  2.    var_export($csv->load('my_cool.csv'));   // true if file is
  3.                                             // loaded

find out if a coordinate is valid

  1.    var_export($csv->hasCell(993))// false

check again for a know valid coordinate and grab that cell

  1.    var_export($csv->hasCell(11));  // true
  2.    var_export($csv->getCell(11));            // '8'

  • Access: public

Parameters:

mixed   $x   —  the row to fetch
mixed   $y   —  the column to fetch

[ Top ]

hasColumn   [line 579]

boolean hasColumn( string $string)

column existance checker

checks if a column exists, columns are identified by their header name.

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

php implementation

  1.    $csv = new File_CSV_DataSource;
  2.    $csv->load('my_cool.csv');
  3.    $headers $csv->getHeaders();

now lets check if the columns exist

  1.    var_export($csv->hasColumn($headers[0]));    // true
  2.    var_export($csv->hasColumn('age'));          // true
  3.    var_export($csv->hasColumn('I dont exist'))// false


Parameters:

string   $string   —  an item returned by getHeaders()

[ Top ]

hasRow   [line 1684]

boolean hasRow( mixed $number)

row existance checker

Scans currently loaded dataset and checks if a given row identified by $number exists

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

load library and csv file

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');

build a relationship and dump it so we can see the rows we will be working with

  1.    var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>  // THIS ROW EXISTS!!!
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

now lets check for row existance

  1.   var_export($csv->hasRow(1));
  2.   var_export($csv->hasRow(-1));
  3.   var_export($csv->hasRow(9999));

output

  1.   true
  2.   false
  3.   false


Parameters:

mixed   $number   —  a numeric value that identifies the row you are trying to fetch.

[ Top ]

isSymmetric   [line 359]

boolean isSymmetric( )

data length/symmetry checker

tells if the headers and all of the contents length match. Note: there is a lot of methods that won't work if data is not symmetric this method is very important!


[ Top ]

load   [line 158]

boolean load( string $filename)

csv file loader

indicates the object which file is to be loaded

  1.    require_once 'File/CSV/DataSource.php';
  2.  
  3.    $csv = new File_CSV_DataSource;
  4.    $csv->load('my_cool.csv');
  5.    var_export($csv->connect());
  6.  
  7.    array (
  8.      0 =>
  9.      array (
  10.        'name' => 'john',
  11.        'age' => '13',
  12.        'skill' => 'knows magic',
  13.      ),
  14.      1 =>
  15.      array (
  16.        'name' => 'tanaka',
  17.        'age' => '8',
  18.        'skill' => 'makes sushi',
  19.      ),
  20.      2 =>
  21.      array (
  22.        'name' => 'jose',
  23.        'age' => '5',
  24.        'skill' => 'dances salsa',
  25.      ),
  26.    )


Parameters:

string   $filename   —  the csv filename to load

[ Top ]

moveHeadersToRows   [line 2214]

void moveHeadersToRows( )

header relocator
  • Access: protected

[ Top ]

parse   [line 2136]

boolean parse( )

csv parser

reads csv data and transforms it into php-data

  • Access: protected

[ Top ]

removeColumn   [line 874]

boolean removeColumn( string $name)

column remover

Completly removes a whole column identified by $name Note: that this function will only work if data is symmetric.

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

load the library and csv file

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');

lets dump currently loaded data

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

and now let's remove the second column

  1.   var_export($csv->removeColumn('age'));

output

  1.   true

those changes made let's dump the data again and see what we got

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'skill' => 'knows magic',
  6.     ),
  7.     1 =>
  8.     array (
  9.       'name' => 'tanaka',
  10.       'skill' => 'makes sushi',
  11.     ),
  12.     2 =>
  13.     array (
  14.       'name' => 'jose',
  15.       'skill' => 'dances salsa',
  16.     ),
  17.   )


Parameters:

string   $name   —  same as the ones returned by getHeaders();

[ Top ]

removeEmpty   [line 2173]

array removeEmpty( )

empty row remover

removes all records that have been defined but have no data.

  • Return: containing only the rows that have data
  • Access: protected

[ Top ]

removeRow   [line 1783]

boolean removeRow( mixed $number)

row remover

removes one row from the current data set.

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

first let's load the file and output whatever was retrived.

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');
  4.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

now lets remove the second row

  1.   var_export($csv->removeRow(1));

output

  1.   true

now lets dump again the data and see what changes have been made

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'jose',
  11.       'age' => '5',
  12.       'skill' => 'dances salsa',
  13.     ),
  14.   )


Parameters:

mixed   $number   —  the key that identifies that row

[ Top ]

resetKeys   [line 2239]

void resetKeys( array &$array)

array key reseter

makes sure that an array's keys are setted in a correct numerical order

Note: that this function does not return anything, all changes are made to the original array as a reference

  • Access: protected

Parameters:

array   &$array   —  any array, if keys are strings they will be replaced with numeric values

[ Top ]

setHeaders   [line 2112]

boolean setHeaders( array $list)

header injector

uses a $list of values which wil be used to replace current headers.

Note: that given $list must match the length of all rows. known as symmetric. see isSymmetric() and getAsymmetricRows() methods

Also, that current headers will be used as first row of data and consecuently all rows order will change with this action.

sample of a csv file "my_cool.csv"

  1.    name,age,skill
  2.    john,13,knows magic
  3.    tanaka,8,makes sushi
  4.    jose,5,dances salsa

load the library and csv file

  1.   require_once 'File/CSV/DataSource.php';
  2.   $csv = new File_CSV_DataSource;
  3.   $csv->load('my_cool.csv');

lets dump currently loaded data

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'name' => 'john',
  5.       'age' => '13',
  6.       'skill' => 'knows magic',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'name' => 'tanaka',
  11.       'age' => '8',
  12.       'skill' => 'makes sushi',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'name' => 'jose',
  17.       'age' => '5',
  18.       'skill' => 'dances salsa',
  19.     ),
  20.   )

And now lets create a new set of headers and attempt to inject them into the current loaded dataset

  1.   $new_headers = array('a''b''c');
  2.   var_export($csv->setHeaders($new_headers));

output

  1.   true

Now lets try the same with some headers that do not match the current headers length. (this should fail)

  1.   $new_headers = array('a''b');
  2.   var_export($csv->setHeaders($new_headers));

output

  1.   false

now let's dump whatever we have changed

  1.   var_export($csv->connect());

output

  1.   array (
  2.     0 =>
  3.     array (
  4.       'a' => 'name',
  5.       'b' => 'age',
  6.       'c' => 'skill',
  7.     ),
  8.     1 =>
  9.     array (
  10.       'a' => 'john',
  11.       'b' => '13',
  12.       'c' => 'knows magic',
  13.     ),
  14.     2 =>
  15.     array (
  16.       'a' => 'tanaka',
  17.       'b' => '8',
  18.       'c' => 'makes sushi',
  19.     ),
  20.     3 =>
  21.     array (
  22.       'a' => 'jose',
  23.       'b' => '5',
  24.       'c' => 'dances salsa',
  25.     ),
  26.   )


Parameters:

array   $list   —  a collection of names to use as headers,

[ Top ]

settings   [line 187]

boolean settings( mixed $array)

settings alterator

lets you define different settings for scanning

Given array will override the internal settings

  1.   $settings = array(
  2.       'delimiter' => ',',
  3.       'eol' => ";",
  4.       'length' => 999999,
  5.       'escape' => '"'
  6.   );

  • Return: true if changes where applyed successfully
  • See: $settings
  • Access: public

Parameters:

mixed   $array   —  containing settings to use

[ Top ]

symmetrize   [line 436]

array symmetrize( [mixed $value = ''])

all rows length equalizer

makes the length of all rows and headers the same. If no $value is given all unexistent cells will be filled with empty spaces


Parameters:

mixed   $value   —  the value to fill the unexistent cells

[ Top ]

validates   [line 2193]

boolean validates( )

csv file validator

checks wheather if the given csv file is valid or not

  • Access: protected

[ Top ]

walkColumn   [line 914]

boolean walkColumn( string $name, string $callback)

column walker

goes through the whole column and executes a callback for each one of the cells in it.

Note: callback functions get the value of the cell as an argument, and whatever that callback returns will be used to replace the current value of that cell.


Parameters:

string   $name   —  the header name used to identify the column
string   $callback   —  the callback function to be called per each cell value

[ Top ]

walkGrid   [line 476]

void walkGrid( string $callback)

grid walker

travels through the whole dataset executing a callback per each cell

Note: callback functions get the value of the cell as an argument, and whatever that callback returns will be used to replace the current value of that cell.


Parameters:

string   $callback   —  the callback function to be called per each cell in the dataset.

[ Top ]

walkRow   [line 1818]

boolean walkRow( string|integer $row, string $callback)

row walker

goes through one full row of data and executes a callback function per each cell in that row.

Note: callback functions get the value of the cell as an argument, and whatever that callback returns will be used to replace the current value of that cell.

  • Return: - false if callback does not exist
    • false if row does not exits
  • Access: public

Parameters:

string|integer   $row   —  anything that is numeric is a valid row identificator. As long as it is within the range of the currently loaded dataset
string   $callback   —  the callback function to be executed per each cell in a row

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:36:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.