PHP library for arrays with tableslike structure (V2.6.1)
- Create from Array, JSON-String, CSV-String, Iterator or XML
- Methods for column selection, row filtering and sorting
- One file, no external requirements
Manually:
- Code -> Download ZIP TableArray-master.zip
- Extract zip to a new Folder
Of course, you can also copy the TableArray source code to your editor and save it.
use Jspit\TableArray;
require '/Path_to_Folder/TableArray.php';
var_dump(TableArray::VERSION); //string(5) "2.6.1"
Install via Composer:
run
composer require jspit/tablearray
use Jspit\TableArray;
require '/Path_to_Composer_vendor_Folder/vendor/autoload.php';
var_dump(TableArray::VERSION); //string(5) "2.6.1"
$data = [
['id' => 1, 'val' => 23.333333333],
['id' => 2, 'val' => 13.7777777777],
];
$newData = TableArray::create($data)
->select('id, FORMAT("%6.2f",val) as rval')
->orderBy('val ASC')
->fetchAll();
$expected = [
['id' => 2, 'rval' => " 13.78"],
['id' => 1, 'rval' => " 23.33"],
];
var_dump($newData == $expected); //bool(true)
$data = [
['name' => 'A1', 'likes' => 3],
['name' => 'A12', 'likes' => 6],
['name' => 'A2','likes' => 14],
['name' => 'A14','likes' => 7],
];
$newData = TableArray::create($data)
->select('name AS class, likes')
->orderBy('name ASC NATURAL')
->fetchAll();
$expected = [
['class' => 'A1', 'likes' => 3],
['class' => 'A2','likes' => 14],
['class' => 'A12', 'likes' => 6],
['class' => 'A14','likes' => 7],
];
var_dump($newData === $expected); //bool(true)
$data = [
['group' => 1, 'type' => 'A', 'value' => 'AA'],
['group' => 2, 'type' => 'A', 'value' => 'BB'],
['group' => 1, 'type' => 'B', 'value' => 5],
['group' => 2, 'type' => 'B', 'value' => 7],
];
$newData = TableArray::create($data)
->pivot('group','value','type')
->fetchAll();
$expected = [
1 => ['group' => 1, 'value.A' => "AA", 'value.B' => 5 ],
2 => ['group' => 2, 'value.A' => "BB", 'value.B' => 7 ],
];
TableArray::setCsvDefaultOptions([
'delimiter'=>',',
'title => true', //use first row as keys
]);
$data = TableArray::createFromCsvFile('file.csv')
->fetchAll()
;
$data = [
['id' => "1",'group' => 1, 'value' => 2, 'value2' => 3],
['id' => "2",'group' => 2, 'value' => 4, 'value2' => 7],
['id' => "3",'group' => 1, 'value' => 1, 'value2' => 2],
['id' => "4",'group' => 2, 'value' => 6, 'value2' => 8],
];
$newData = TableArray::create($data)
->filterGroupAggregate(['value' => 'MAX', 'value2' => 'AVG'],['group'])
->orderBy('value2 DESC')
->fetchAll();
$expected = [
['id' => "4",'group' => 2, 'value' => 6, 'value2' => 7.5],
['id' => "1",'group' => 1, 'value' => 2, 'value2' => 2.5],
];
var_dump($newData === $expected); //bool(true)
- new TableArray ($dataArray,[$keyPathToData])
- create ($dataArray,[$keyPathToData])
- createFromJson ($jsonStr,[$keyPathToData])
- createFromXml ($xml, [$strXPath])
- createFromOneDimArray ($dataArray,[$delimiter])
- createFromString ($inputString, [$regExValues,[$regExSplitLines]])
- createFromGroupedArray($input, $keyArray)
- createFromCsvFile([$file])
- filter
- filterEqual
- filterLikeAll
- filterLikeIn
- filterUnique
- filterGroupAggregate
- fetchAll
- fetchAllObj
- fetchAllAsJson
- fetchAllAsCSV
- fetchKeyValue
- fetchColumn
- fetchColumnUnique
- fetchGroup
- fetchRow
- fetchRaw
- fetchLimit
- fetchLimitFromEnd
- addFlatKeys
- addKeys
- fieldAsKey
- firstRowToKey
- addSqlFunction
- addSqlFunctionFromArray
- getSqlFunction
- fieldNameRaw
- setOption
- getOption
- setCsvDefaultOptions($options)
- check($data)
- unGroup($array, $keys)
- count
- toClass
- print($comment,$limit)
- ABS(fieldName)
- UPPER(fieldName)
- FIRSTUPPER(fieldName)
- LOWER(fieldName)
- TRIM(fieldName[,'character_mask'])
- FORMAT('format',fieldName[,fieldName])
- SCALE(fieldName,'factor'[,'add'[,'format']])
- DATEFORMAT('dateFormat',fieldName)
- REPLACE('search','replace',fieldName)
- SUBSTR(fieldName,'start'[,'length'])
- LIKE(fieldName,'likePattern')
- INTVAL(fieldName,'basis')
- FLOATVAL(fieldName,['dec_point', 'thousands_sep'])
- NULLCOUNT(fieldName[,fieldName,..])
- CONCAT(fieldName[,fieldName,..])
- IMPLODE(arrayFieldName,['delimiter'])
- SPLIT(fieldName[,'delimiter'[,'number']])
- Iterator
- JsonSerializable
Select rows for a fetch ->select('field1, field2,..') ->select('field1 as newName,..') ->select('fct(field1) as newName,..)
$data =[
['id' => 1, 'article' => "pc1", 'price' => 1231.0],
['id' => 1, 'article' => "pc2", 'price' => 471.5],
];
$newData = TableArray::create($data)
->select("article as Name, FORMAT('%.2f€',price) as Euro")
->fetchAll()
;
/* Result $newData
[
['Name' => "pc1", 'Euro' => "1231.00€"],
['Name' => "pc2", 'Euro' => "471.50€",
]
*/
Sorts the array by one or more columns in ascending or descending order.
->orderBy('field1 [ASC|DESC][NATURAL], [field2..]')
->orderBy('fct(field1,[params]),[field|function..])
http://jspit.de/tools/classdoc.php?class=TableArray
http://jspit.de/check/phpcheck.class.tablearray.php
- PHP 7.x