Csv
in package
CSV Utility Class
Tags
Table of Contents
- $charset : mixed
- $columns : mixed
- $datas : mixed
- $keys : mixed
- import() : array<string|int, mixed>
- Import CSV data
- read() : void
- Read a CSV file and process each row of data
- send() : bool
- Generate and send a CSV file as a download
- convert() : array<string|int, mixed>
- Convert data array to the specified character encoding
- importDatas() : void
- Import data and process into a specific format
- removeBomUtf8() : string
- Remove the Byte Order Mark (BOM) from a UTF-8 encoded string
Properties
$charset
private
mixed
$charset
$columns
private
mixed
$columns
$datas
private
mixed
$datas
$keys
private
mixed
$keys
Methods
import()
Import CSV data
public
static import(string $csv, array<string|int, mixed> $columns[, array<string|int, mixed> $keys = null ][, string $charset = 'UTF-8' ]) : array<string|int, mixed>
Parameters
- $csv : string
-
File path
- $columns : array<string|int, mixed>
-
Column data array('column1' => 'data type', 'column2' => 'data type', ....)
- $keys : array<string|int, mixed> = null
-
Column names for duplicate data checking. Null(default) means no checking.
- $charset : string = 'UTF-8'
-
File character encoding. Default is UTF-8.
Return values
array<string|int, mixed> —Imported data array
read()
Read a CSV file and process each row of data
public
static read(string $file, callable $onRow[, array<string|int, mixed> $headers = null ][, string $charset = 'UTF-8' ]) : void
Parameters
- $file : string
-
Path to the CSV file
- $onRow : callable
-
Callback function to be executed for each row of data
- $headers : array<string|int, mixed> = null
-
Array of expected header values for validation (optional)
- $charset : string = 'UTF-8'
-
Character encoding of the CSV file (default: UTF-8)
Tags
Return values
void —send()
Generate and send a CSV file as a download
public
static send(string $file, array<string|int, mixed> $header, array<string|int, mixed> $datas[, string $charset = 'UTF-8' ][, bool $bom = true ]) : bool
Parameters
- $file : string
-
File name (without extension)
- $header : array<string|int, mixed>
-
Array of header values for the CSV file
- $datas : array<string|int, mixed>
-
Array of data rows for the CSV file
- $charset : string = 'UTF-8'
-
Character encoding of the CSV file (default: UTF-8)
- $bom : bool = true
-
Whether to include the Byte Order Mark (BOM) in the CSV file (default: true)
Return values
bool —Returns true if successful, false otherwise
convert()
Convert data array to the specified character encoding
private
static convert(array<string|int, mixed> $datas, string $charset) : array<string|int, mixed>
Parameters
- $datas : array<string|int, mixed>
-
Data array to convert
- $charset : string
-
Target character encoding
Return values
array<string|int, mixed> —Converted data array
importDatas()
Import data and process into a specific format
private
importDatas(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
-
The data array to be imported
Return values
void —removeBomUtf8()
Remove the Byte Order Mark (BOM) from a UTF-8 encoded string
private
static removeBomUtf8(string $s) : string
Parameters
- $s : string
-
UTF-8 encoded string
Return values
string —String with BOM removed