jspit / tablearray
PHP库,用于具有表格结构的数组
v2.6.1
2022-01-19 13:15 UTC
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-09-26 23:15:09 UTC
README
PHP库,用于具有表格结构的数组(V2.6.1)
特性
- 从数组、JSON字符串、CSV字符串、迭代器或XML创建
- 用于列选择、行过滤和排序的方法
- 单个文件,无外部要求
安装与加载
手动
- 代码 -> 下载 ZIP TableArray-master.zip
- 将ZIP文件解压到新文件夹
当然,您也可以将TableArray源代码复制到您的编辑器并保存。
use Jspit\TableArray; require '/Path_to_Folder/TableArray.php'; var_dump(TableArray::VERSION); //string(5) "2.6.1"
通过Composer安装
运行
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"
用法
简单示例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)
简单示例2
$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 ], ];
CSV导入示例
TableArray::setCsvDefaultOptions([ 'delimiter'=>',', 'title => true', //use first row as keys ]); $data = TableArray::createFromCsvFile('file.csv') ->fetchAll() ;
示例 filterGroupAggregate
$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)
内部函数可以由select和orderBy使用
- 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
选择要检索的行 ->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€", ] */
orderBy
按一个或多个列以升序或降序排序数组。
->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