Skip to content

jflores82/class_csv_header

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

class.csvheader.php

PHP CSV Reader Class

This is a simple PHP class to load and parse .csv files. Files can be separated with any character and enclosed with any character. New lines must be \r\n as per most OS's requirements.

This class was coded to make it easy to load and read csv files without worrying about loops and what not. It returns a readable array.

Usage

require_once('class.csvreader.php');
$csv = new csvreader($csv_file);

Methods:

->ReadCSV:

Returns the entire CSV file, in order.

Arguments:

offset: optional, int, Default 1

separator: optional, string, Default ;

enclosure: optional, string, Default "

ex.

$read_entire = $csv->ReadCSV(1, ";", "'");
var_dump($read_entire) // Entire CSV file with header (offset = 1) and data enclosed by "", while separated by ;

Returns:

array(
	[0]->[0]->field 1, [1]->field 2, [2]->field 3
	[1]->[0]->field 1, [1]->field 2, [2]->field 3
)

->ReadFilterCSV

Returns the CSV file, but only the lines where column filterfield matches filter Column numbers starts at zero. So, 0 = first column, 1 = second column and so on. Acceps an offset number, to skip the first lines, most used with headers.

Arguments:

filterfield: required, int, num of column to be read

filter: required, string, what to match

offset: optional, int, number of lines to offset.

separator: optional, string, Default ;

enclosure: optional string, Default "

ex.

$read_filtered = $csv->ReadCSVFilter(1, "foo");
var_dump($read_filtered) // Array containing only the rows in which the column 1 (second column) has the value "foo"

Returns:

array(
	[0]->[0]->field 1, [1]->field 2, [2]->field 3
	[1]->[0]->field 1, [1]->field 2, [2]->field 3
)

->CountCSV

Returns a integer with the number of lines in the csv file. Accepts an offset value, in case you want to skip a header.

Arguments:

offset : optional, int, Default 0

separator: optional, string, Default ;

enclosure: optional string, Default "

ex.

$numlines = $csv->CountCSV(1, ";", "'");
var_dump($numlines) // int: number of lines after the first;

Returns:

$numlines: int 1

->CountFilterCSV

Returns a integer with the number of line in the csv where filterfield matches filter Accepts an offset value, in case you want to skip a header.

Arguments:

filterfield: required, int, num of column to be read

filter: required, string, what to match

offset: optional, int, Default 0

separator: optional, string, Default ;

enclosure: optional string, Default "

ex.

$numlines = $csv->CountCSV(1, "foobar", 0, ";", "'");
var_dump($numlines) // int: number of lines that the second column (1) has the content "foobar"

Returns:

$numlines: int 1

-> minmaxNumCSV

Return an array, with the minimum and maximum values found on a numeric specified field. Accepts an offset value, in case you want to skip a header.

Arguments

filterfield: required, int, num of column to be read

offset: optional, int, Default 0

separator: optional, string, Default ;

enclosure: optional string, Default "

ex.

$minmax_value = $csv->minmaxNumCSV(1,1);
var_dump($minmax_value) // array (0 -> minimum, and 1 -> maximum)

Returns:

$minmax_valeu: Array([0]->0, [1]->100)

-> minmaxAlphaCSV

Return an array, with the minimum and maximum values found on an alpha-numeric specified field. Accepts an offset value, in case you want to skip a header.

Arguments

filterfield: required, int, num of column to be read

offset: optional, int, Default 0

separator: optional, string, Default ;

enclosure: optional string, Default "

ex.

$minmax_value = $csv->minmaxAlphaCSV(1,1);
var_dump($minmax_value) // array (0 -> minimum, and 1 -> maximum)

Returns:

$minmax_value: Array([0]->a, [1]->z)

-> countGroupCSV

Returns an array, with the key being the all the values found in a specified column and the value being the number of times this key was found Accepts an offset value, in case you want to skip a header.

Arguments

filterfield: required, int, num of column to be read

ascdesc: optional, int, if the group will be ordered ascending or descending. 0 = ascending, 1 = descending. Default: ascending

limit: optional, int, limit the number of returned rows. The number are the number of first rows returned.

offset: optional, int, Default 0

separator: optional, string, Default ;

enclosure: optional string, Default "

ex.

$group_return = $csv->countGroupCSV(1,1,1,5);  // Returns the first five rows, skipping the first, in descending order. Column number 1.
var_dump($group) // array (key = something, value = number of something)

Returns:

$group_return: Array([foo]->2, [bar]->1, [foobar]->0)

-> avgFieldCSV

Returns an int. Which is the average of the values on a given numeric column. Accepts an offset value, in case you want to skip a header.

Arguments:

field: required, int, num of column to be read

offset: optional, int, Default 0

ex.

$avg = $csv->avgFieldCSV(1,1); // Returns the average of values on the first column, given a header of 1.
var_dump($avg); // int.

Returns:

$csv: int (Value)

Known Bugs

None.

About

PHP CSV Reader Class

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages