Download the PHP package eftec/arrayone without Composer
On this page you can find all versions of the php package eftec/arrayone. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package arrayone
ArrayOne
It is a minimalist library that process arrays in PHP with no dependency.
This library is focused to work with business data(reading/saving files, database records, API, etc.), so it is not similar to Numpy, Pandas, NumPHP or alike because they target difference objectives. It is more closely similar to Microsoft PowerQuery and Linq. What it does? Filter, order, renaming column, grouping, validating, amongst many other operations.
- [x] it works with PHP arrays. PHP arrays allows hierarchy structures using indexed and/or associative values.
- [x] It is aimed at speed.
- [x] It is minimalist, using the minimum of dependencies and only 1 PHP class. Do you hate when a simple library adds a whole framework as dependency? Well, not here.
- [x] It works using fluent/nested notations.
- [x] Every method is documented using PhpDoc.
- ArrayOne
- Basic examples
- Getting started
- Concepts
- initial operator
- set
- setRequest
- setJson
- setCsv
- setCsvHeadless
- middle operator
- col
- columnToIndex
- filter
- find
- isIndexArray() isIndex()
- isIndexTableArray() isIndexTable()
- first
- flat
- group
- example
- indexToCol
- join
- last
- map
- mask
- npos
- reduce
- removecol
- removerow
- removeFirstRow
- removeLastRow
- removeDuplicate
- modCol
- sort
- createValidateExample
- validate
- end operators
- getAll()
- isValid()
- other methods
- makeValidateArrayByExample
- makeRequestArrayByExample
- versions
- License
Basic examples
Getting started
First, you must install the library. You can download this library or use Composer for its installation:
composer require eftec/arrayone
Once the library is installed and included, you can use as:
Concepts
- indexed and named fields works similarly.
- Sometimes, some field contains an array of values that behave like a table (see table field)
initial operator
Initial operator is the first operator of the chain.
set
It sets the array to be transformed, and it starts the pipeline. It must be the first operator unless you are using the constructor.
- parameter array|null $array
- parameter object|null|string $service the service instance. You can use the class or an object.
setRequest
It sets the initial array readint the values from the request (get/post/header/etc.)
Example:
- parameter array $fields An associative array when the values to read 'id'=>'type;defaultvalue'.
Types:
get: get it from the query string
post: get it from the post
header: get if from the header
request: get if from the post, otherwise from get
cookie: get if from the cookies
body: get if from the post body (values are not serialized)
verb: get if from the request method (GET/POST/PUT,etc.) - parameter mixed $defaultValueAll the default value if the value is not found and not other default value is set.
- parameter ?string $separator Def:'.', The separator character used when the field is nested.
example using '.' as separator html:
result obtained:$result['a']['b']='hello'; - return value ArrayOne
setJson
It sets the array using a json. Example:
- parameter string $json the value to parse.
setCsv
It sets the array using a csv. This csv must have a header.
Example:
- parameter string $string the string to parse
- parameter string $separator default ",". Set the field delimiter (one character only).
- parameter string $enclosure default '"'. Set the field enclosure character (one character only).
- parameter string $escape default "\". Set the escape character (one character only).
setCsvHeadless
It sets the array using a head-less csv.
Example:
- parameter string $string the string to parse
- parameter array|null $header If the header is null, then it creates an indexed array.
if the header is an array, then it is used as header - parameter string $separator default ",". Set the field delimiter (one character only).
- parameter string $enclosure default '"'. Set the field enclosure character (one character only).
- parameter string $escape default "\". Set the escape character (one character only).
middle operator
Middle operators are operators that are called between the initial operator set() and the end operator all() or getCurrent(). They do the transformation and they could be stacked.
example:
col
Returns a single column as an array of values.
Example:
columnToIndex
it converts a column into an index
Example:
- parameter mixed $oldColumn the old column. This column will be converted into an index
filter
It filters the values. If the condition is false, then the row is deleted. It uses array_filter()
The indexes are not rebuilt.
Example:
You can find an example in examplefindandfilter Note: see validate for more information about conditions.
find
It returns an array with the key and values of the elements that matches the condition.
- parameter callable|null|array $condition you can use a callable function ($row,$id):bool {}
or a comparison array ['id'=>'eq;2|lt;3'] "|" adds more comparisons
or a comparison array [['id=>['eq',2]]],['id'=>['lt',3]] - parameter bool $onlyFirst if true then it only returns the first value
- parameter string $mode =['all','key','value'] // (default is all)
all returns the key and the value obtained
key only returns the key
value only returns the value
Example:
It uses the same conditions as filter()
isIndexArray() isIndex()
Returns true if the array is an indexed array. It does not scan the whole array, but instead it only returns
true if the index 0 exists, and it is the first value.
Example:
isIndexTableArray() isIndexTable()
It returns true if the value is an indexed array with the first value an array (i.e. a table) Example:
first
It returns the first element of an array.
-
return value $this
flat
It flats the results. If the result is an array with a single row, then it returns the row without the array
Example: - return value $this
group
It groups one column and return its column grouped and values aggregated.
The grouped value is used as the new index key.
Example:
- parameter mixed $column the column to group.
- parameter array $functionAggregation An associative array ['col-to-agregate'=>'aggregation']
or ['new-col'=>'aggregation(col-to-agregate)']
or ['col-to-aggr'=>function($cumulate,$row) {}]
or ['col-to-aggr'=>[function($cumulate,$row){},function($cumulate,$numRows){}]
stack: It stacks the rows grouped by the column (like a pivot table).
count: Count
avg: Average
min: Minimum
max: Maximum
sum: Sum
first: First
last: last
group: The grouped value - parameter bool $useGroupAsIndex (def true), if true, then the result will use the grouped value as index
if false, then the result will return the values as an indexed array.example
indexToCol
It converts the index into a field, and renumerates the array
Example:
- parameter mixed $newColumn the name of the new column
-
return value $this
join
Joins the current array with another array
If the columns of both arrays have the same name, then the current name is retained.
Example: - parameter array|null $arrayToJoin
- parameter mixed $column1 the column of the current array
- parameter mixed $column2 the column of the array to join.
- return value $this
last
It returns the last element of an array.
-
return value $this
map
It calls a function for every element of an array. Example:
- parameter callable|null $condition The function to call.
-
return value $this
mask
It masks the current array using another array.
Masking deletes all field that are not part of our mask
The mask is smart to recognize a table, so it could mask multiples values by only specifying the first row.
Example: - param array $arrayMask An associative array with the mask. The mask could contain any value.
- return value ArrayOne
npos
It returns the n-position of an array.
- parameter $index
-
return value $this
reduce
You can reduce (flat) an array using aggregations or a custom function. Example:
- parameter array|callable $functionAggregation An associative array where the index is the column and the value
is the function of aggregation
A function using the syntax: function ($row,$index,$prev) where $prev is the accumulator value - return value $this
removecol
It removes a column
Example:
- parameter mixed $colName The name of the column or columns (array)
- return value $this
removerow
It removes the row with the id $rowId. If the row does not exist, then it does nothing Example:
- parameter mixed $rowId The id of the row to delete
- parameter bool $renumber if true then it renumber the list
ex: if 1 is deleted then $renumber=true: [0=>0,1=>1,2=>2] => [0=>0,1=>2]
ex: if 1 is deleted then $renumber=false: [0=>0,1=>1,2=>2] => [0=>0,2=>2] - return value $this
removeFirstRow
It removes the first row or rows. Numeric index could be renumbered. Example:
- parameter int $numberOfRows The number of rows to delete, the default is 1 (the first row)
- parameter bool $renumber if true then it renumber the list
ex: if 1 is deleted then $renumber=true: [0=>0,1=>1,'x'=>2] => [0=>0,1=>2]
ex: if 1 is deleted then $renumber=false: [0=>0,1=>1,2=>2] => [0=>0,2=>2] - return value $this
removeLastRow
It removes the last row or rows Example:
- parameter int $numberOfRows the number of rows to delete
- parameter bool $renumber if true then it renumber the list (since we are deleting the last value then
usually we don't need it
ex: if 1 is deleted then $renumber=true: [0=>0,1=>1,2=>2] => [0=>0,1=>2]
ex: if 1 is deleted then $renumber=false: [0=>0,1=>1,2=>2] => [0=>0,2=>2] - return value $this
removeDuplicate
This function removes duplicates of a table.
Example:
- parameter mixed $colName the column to compare if the rows are duplicated.
- return value $this
modCol
It adds or modify a column. Example:
- parameter string|int|null $colName the name of the column. If null, then it uses the entire row
- parameter callable|null $operation the operation to realize.
-
return value $this
sort
Sort an array
Example: - parameter mixed $column if column is null, then it sorts the row (instead of a column of the row)
- parameter string $direction =['asc','desc'][$i] ascending or descending.
- return value $this
createValidateExample
It creates a validation array using an example
validate
Validate the current array using a comparison table
Example:
Example Using a custom function:
- parameter array $comparisonTable The comparison table, is an associative table with the conditions to compare using the next syntax: [index=>"condition|condition2..."].
- The conditions could be express as: \
;\ ;\ - If the \
starts with "not", then it is negated, example: "notalpha" - The \
could be a simple literal (1,hello, etc.) or a list of values (separated by comma) - The \
could contain the next variables: %field the id of the field, %value the current value of the field, %comp the value to compare, %first the first value to compare, %second the second value to compare, example "%field (%value) is not equals to %comp", %rowid is the id of the current row (if any)
- The conditions could be express as: \
condition | description | example work | example fail | expression |
---|---|---|---|---|
not\ |
negates any comparison, excepting nullable and custom functions. Example: "notint" | "hello" | 20 | notint |
nullable | the value CAN be a null. If the value is null, then it ignores other validations | null | nullable | |
f:\ |
It calls a custom function defined in the service class. See example | "hello" | f:test | |
contain like | if a text is contained in | "helloworld" | "hello" | contain;world |
alpha | if the value is alphabetic | "hello" | "hello33" | alpha |
alphanumunder | if the value is alphanumeric or under-case | "hello_33" | "hello!33" | alphanumunder |
alphanum | if the value is alphanumeric | "hello33" | "hello!33" | alphanum |
text | if the value is a text | "hello" | true | text |
regexp | if the value match a regular expression. You can't use comma in the regular expression. | "abc123" | "xyz123" | regexp;/abc*/ |
if the value is an email | "[email protected]" | "aaa.bbb.com" | ||
url | if the value is an url | https://www.nic.cl | "aaaa" | url |
domain | if the value is a domain | www.nic.cl | "….." | domain |
minlen | the value must have a minimum length | "hello" | "h" | minlen;3 |
maxlen | the value must have a maximum lenght | "h" | "hello" | maxlen;3 |
betweenlen | if the value has a size between | "hello" | "h" | betweenlen;4,5 |
exist | if the value exists | "hi" | null | exist |
missing | if the value not exist | null | "hi" | missing |
req,required | if the value is required | "hi" | null | req,required |
eq == | if the value is equals to | 1 | 0 | eq;1 |
ne != <> | if the value is not equals to | 1 | 0 | ne;0 |
null | The value MUST be null. It is different to nullable because nullable is a "CAN" | null | "hello" | null |
empty | if the value is empty | "" | "hello" | empty |
lt | if the value is less than | 1 | 10 | lt;5 |
lte/le | if the value is less or equals than | 1 | 10 | lte;5 |
gt | if the value is great than | 10 | 1 | gt;5 |
gte/ge | if the value is great or equals than | 10 | 1 | gte;5 |
between | if the value is between | 5 | 0 | between;4,5 |
true | if the value is true or 1 | true | false | true |
false | if the value is false, or 0 | false | true | false |
array | if the value is an array | [1,2,3] | 1 | array |
int | if the value is an integer | 1 | "hello" | int |
string | if the value is a string | "hello" | true | string |
float | if the value is a float | 333.3 | "hello" | float |
object | if the value is an object | new stdClass() | 1 | object |
in | the value must be in a list | "a" | "x" | in;a,b,c |
- parameter bool $extraFieldError if true and the current array has more values than comparison table, then it returns an error.
end operators
getAll()
Returns the whole array transformed.
Example:
isValid()
returns true if the validation has no error.
Example:
Note: you can also obtain the result of the validation: ->valid($arrayc)->all();
Note2: If you want a simple array with errors, you can use $this::errorStack;
Example:
- return value mixed
other methods
Methods that does not fit in the other categories. Those methods are not stackable.
makeValidateArrayByExample
It generates a validate-array using an example array. It could be used by validation() and filter()
Example:
- parameter array $array
- return value array
makeRequestArrayByExample
It creates an associative array that could be used to be used by setRequest()
Example:
- parameter array $array An associative array with some values.
- parameter string $type=['get','post','request','header','cookie'][$i] The default type
- return value array
versions
- 2.4 2024-08-15
- [update] sort() now allow to group by multiples columns
- [fixed] group() fixed the value returned. If multiples columns are used then, the new grouping column could be split.
- [new] splitColumn() split a column in two or more columns.
- 2.3 2024-08-10
- [new] sum(),min(),max(),avg(),count(),aggr()
- 2.2 2024-08-06
- sort() now accepts multiple columns.
- 2.1 2024-08-03
- removeDuplicate(),group() now accepts multiples columns.
-
2.00 2024-03-10
- nav() and currentArray() are removed from 2.0. The library was optimized and streamlined, and those functions are redundant.
migration:
- nav() and currentArray() are removed from 2.0. The library was optimized and streamlined, and those functions are redundant.
- 1.12 2024-03-01
- Updating dependency to PHP 7.4. The extended support of PHP 7.2 ended 3 years ago.
- 1.11 2024-03-01
- added method find()
- aedded method isIndexArray() and isIndexTableArray()
- now find() and filter() allows multiple conditions
- and find() and filter(), the condition ['field'='eq;2'] could be written as ['field','2']
- 1.10 2024-02-24
- Added more doc for validate()
- Now validate also returns an array $this::$errorStack
- New method isValid() which returns true is validate has no error. Otherwise false.
- 1.9 2023-11-13
- added rowToValue()
- 1.8.3 2023-09-16
- offsetGet() generates a warning in php 8.1 (fixed)
- current() is marked as deprecated (but it is still functional), use getCurrent()
- 1.8.2 2023-09-16
- solved a psr-4 problem in composer.json
- 1.8.1 2023-09-16
- change the PHPDOC comments, now it uses markdown instead of "pre" tag.
- Added ArrayAccess interface.
- 1.8 2023-07-29
- [mod] group() allows to specify a custom function(s).
- 1.7 2023-06-04
- [new] group() allows returning the grouped value. It also allows returning the values as an indexed array.
- 1.6 2023-04-10
- [optimization] setCurrentArray() now is only used when nav() is called or when the value is returned.
- 1.5 2023-04-07
- [new] filtercondition() now allow conditions as array.
- 1.4 2023-04-05
- [fix] filtercondition() fixed a warning when the value is null.
- [new] group() now allow to stack elements
- [new] group() now allow to specify a new column
- 1.3 2023-03-31
- validation now allow negation ("not" prefix).
- 1.2
- renamed method getValidateArrayByExample() to makeValidateArrayByExample()
- new method makeRequestArrayByExample()
- new method setRequest()
- rename method setCol() to modCol(). Methods that start with "set" are used to initialize the variable.
- 1.1 2023-03-28
- method filter() now allow a comparison array and a callable function.
- new method getValidateArrayByExample()
- new method removeRow()
- new method removeFirstRow()
- new mehtod removeLastRow()
- new method setCsv()
- new method setJson()
- 1.0 2023-03-26 first version
License
Copyright Jorge Castro Castillo 2023-2024. Licensed under dual license: LGPL-3.0 and commercial license.
In short:
- [x] Can I use in a close source application for free? Yes if you don't modify this library.
- [x] If you modify it, then you must share the source code.
- [x] If you want to modify privately, then you must buy a license.
All versions of arrayone with dependencies
ext-ctype Version *
ext-json Version *