Recommend this page to a friend! |
It is a minimalist library that process arrays in PHP.
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. What it does? Filter, order, renaming column, grouping, validating, amongst many other operations.
<!-- TOC --> * ArrayOne * Basic examples * Getting started * Concepts * initial operator
* set
* col
* columnToIndex
* filter
* first
* flat
* group
* example
* indexToColumn
* join
* last
* map
* mask
* nav
* npos
* reduce
* removecol
* removeDuplicate
* setCol
* sort
* validate
* all
* result
* versions <!-- TOC -->
// Reducing an array using aggregate functions:
$invoice=[
'id'=>1,
'date'=>new DateTime('now'),
'customer'=>10,
'detail'=>[
['idproduct'=>1,'unitPrice'=>200,'quantity'=>3],
['idproduct'=>2,'unitPrice'=>300,'quantity'=>4],
['idproduct'=>3,'unitPrice'=>300,'quantity'=>5],
]
];
$arr=ArrayOne::set($invoice)
->nav('detail')
->reduce(['unitPrice'=>'sum','quantity'=>'sum'])
->current(); //['unitPrice'=>800,'quanty'=>12]
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:
use eftec\ArrayOne;
ArrayOne::set($array); // Initial operator: $array is our initial array.
->someoperator1() // Middle operator: here we do one or many operations to transform the array
->someoperator2()
->someoperator3()
->current(); // End operator: and we get the end result that usually is an array but it could be even a literal.
$array=['hello' // indexed field
'field2'=>'world', // named field
'fields'=>[ // a field with sub-fields
'alpha'=>1,
'beta'=>2
],
'table'=>[ // a field with a list of values (a table)
['id'=>1,'name'=>'red'],
['id'=>2,'name'=>'orange'],
['id'=>3,'name'=>'blue'],
]
];
Initial operator is the first operator of the chain.
It sets the array to be transformed, and it starts the pipeline. It must be the first operator unless you are using the constructor.
ArrayOne::set($array)->all();
// or you can use the constructor.
(new ArrayOne($array))->all();
Middle operators are operators that are called between the initial operator set() and the end operator all() or current(). They do the transformation and they could be stacked.
example:
ArrayOne::set($array)
->nav('field') // middle operator #1
->group('col1',['col2'=>'sum']) // middle operator #2
->all();
Returns a single column as an array of values. Example:
$this->col('c1'); // [['c1'=>1,'c2'=>2],['c1'=>3,'c2'=>4]] => [['c1'=>1],['c1'=>3]];
it converts a column into an index Example:
$this->indexToField('colold'); // [['colold'=>'a','col1'=>'b','col2'=>'c'] => ['a'=>['col1'=>'b','col2'=>'c']]
It filters the values. If the condition is false, then the row is deleted. It uses array_filter() The indexes are not rebuilt. Example:
$array = [['id' => 1, 'name' => 'chile'], ['id' => 2, 'name' => 'argentina'], ['id' => 3, 'name' => 'peru']];
// ['id' => 2, 'name' => 'argentina']
$r = ArrayOne::set($array)->filter(function($id, $row) {return $row['id'] === 2;}, true)->current();
// [1=>['id' => 2, 'name' => 'argentina']]
$r = ArrayOne::set($array)->filter(function($id, $row) {return $row['id'] === 2;}, false)->current();
It returns the first element of an array. return value* $this
It flats the results. If the result is an array with a single row, then it returns the row without the array Example:
$this->flat(); // [['a'=>1,'b'=>2]] => ['a'=>1,'b'=>2]
return value $this
### group
It groups one column and return its column grouped and values aggregated
Example:
$this->group('type',['amount'=>'sum','price'=>'sum']);
$array=[
['cat'=>'cat1','col_min'=>1,'col_max'=>1,'col_sum'=>1,'col_avg'=>1,'col_first'=>'john1','col_last'=>'doe1'],
['cat'=>'cat2','col_min'=>2,'col_max'=>2,'col_sum'=>2,'col_avg'=>2,'col_first'=>'john2','col_last'=>'doe2'],
['cat'=>'cat3','col_min'=>3,'col_max'=>3,'col_sum'=>3,'col_avg'=>3,'col_first'=>'john3','col_last'=>'doe3'],
['cat'=>'cat1','col_min'=>4,'col_max'=>4,'col_sum'=>4,'col_avg'=>4,'col_first'=>'john4','col_last'=>'doe4'],
['cat'=>'cat2','col_min'=>5,'col_max'=>5,'col_sum'=>5,'col_avg'=>5,'col_first'=>'john5','col_last'=>'doe5']
];
$result=ArrayOne::set($array)
->group('cat',[
'col_min'=>'min',
'col_max'=>'max',
'col_sum'=>'sum',
'col_avg'=>'avg',
'col_count'=>'count',
'col_first'=>'first',
'col_last'=>'last',
])
->all();
/* [
'cat1' =>
['col_min' => 1, 'col_max' => 4, 'col_sum' => 5, 'col_avg' => 2.5, 'col_first' => 'john1', 'col_last' => 'doe4', 'col_count' => 2,],
'cat2' =>
['col_min' => 2, 'col_max' => 5, 'col_sum' => 7, 'col_avg' => 3.5, 'col_first' => 'john2', 'col_last' => 'doe5', 'col_count' => 2,],
'cat3' =>
['col_min' => 3, 'col_max' => 3, 'col_sum' => 3, 'col_avg' => 3, 'col_first' => 'john3', 'col_last' => 'doe3', 'col_count' => 1,],
];
It converts the index into a field, and renumerates the array Example:
$this->indexToField('colnew'); // ['a'=>['col1'=>'b','col2'=>'c']] => [['colnew'=>'a','col1'=>'b','col2'=>'c']
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:
$products=[['id'=>1,'name'=>'cocacola','idtype'=>123]];
$types=[['id'=>123,'desc'=>'it is the type #123']];
ArrayOne::set($products)->join($types,'idtype','id')->all()
// [['id'=>1,'prod'=>'cocacola','idtype'=>123,'desc'=>'it is the type #123']] "id" is from product.
return value $this
### mask
It masks the current array using another array.<br>
Masking deletes all field that are not part of our mask<br>
The mask is smart to recognize a table, so it could mask multiples values by only specifying the first row.
Example:
$array=['a'=>1,'b'=>2,'c'=>3,'items'=>[[a1'=>1,'a2'=>2,'a3'=3],[a1'=>1,'a2'=>2,'a3'=3]];
$mask=['a'=>1,'items'=>[[a1'=>1]]; // [[a1'=>1]] masks an entire table
$this->mask($mask); // $array=['a'=>1,'items'=>[[a1'=>1],[a1'=>1]];
return value ArrayOne
### nav
Navigate inside the arrays.
If you want to select a subcolumn, then you could indicate it separated by dot: "column.subcolumn". You
can separate up to 5 levels.
Example:
$this->nav('col');
$this->nav(); // return to root.
$this->nav('col.subcol.subsubcol'); // [col=>[subcol=>[subsubcol=>[1,2,3]]]] returns [1,2,3]
return value $this
### reduce
You can reduce (flat) an array using aggregations or a custom function.
Example:
$this->reduce(['col1'=>'sum','col2'=>'avg','col3'=>'min','col4'=>'max']);
$this->reduce(function($row,$index,$prev) { return ['col1'=>$row['col1']+$prev['col1]]; });
return value $this
### removecol
It removes a column
Example:
$this->removeCol('col1');
$this->removeCol(['col1','col2']);
return value $this
### removeDuplicate
### setCol
It adds or modify a column.
Example:
$this->setCol('col1',function($row,$index) { return $row['col2']*$row['col3']; });
return value $this
### sort
Sort an array
Example:
$this->sort('payment','desc'); // sort an array using the column paypent descending.
return value $this
### validate
Validate the current array using a comparison table
Example:
$this->validate([
'id'=>'int',
'price'=>'int|between;1,20' // the price must be an integer and it must be between 1 and 20 (including them).
'table'=>[['col1'=>'int';'col2'=>'string|notnull,,the value is required|']], // note the double [[ ]] to indicate a table of values
'list'=>[['int']]
]);
Example Using a custom function:
// 1) defining the service class.
class ServiceClass {
/
* @param mixed $value the value to evaluate
* @param mixed $compare the value to compare (optional)
* @param ?string $msg the message to return if fails
* @return bool
*/
public function test($value,$compare=null,&$msg=null): bool
{
return true;
}
}
// 2.1) and setting the service class using the class
ValidateOne
->set($array,ServiceClass:class)
->validate('field'=>'fn:test')
->all();
// 2.2) or you could use an instance
$obj=new ServiceClass();
ValidateOne
->set($array,$obj)
->validate('field'=>'fn:test')
->all();
| Name | Description | syntax | |-------------------|-----------------------------------------------------------------------------------------------------|------------------------------| | nullable | the value can be a null. If the value is null, then it ignores other validations | nullable | | f:\<namefunction> | It calls a custom function defined in the service class. See example | f:myfunction | | contain | if a text is contained in | contain;\<text> | | notcontain | if a text is not contained in | notcontain;\<text> | | alpha | if the value is alphabetic | alpha | | alphanumunder | if the value is alphanumeric or under-case | alphanumunder | | alphanum | if the value is alphanumeric | alphanum | | text | if the value is a text | text | | regexp | if the value match a regular expression. You can't use comma in the regular expression. | regexp;\<regular expression> | | email | if the value is an email | email | | url | if the value is an url | url | | domain | if the value is a domain | domain | | minlen | the value must have a minimum length | minlen;\<size> | | maxlen | the value must have a maximum lenght | maxlen;\<size> | | betweenlen | if the value has a size between | betweenlen;<sizemin,sizemax> | | exist | if the value exists | exist | | missing,notexist | if the value not exist | missing\ |notexist | | req,required | if the value is required | req\ |required | | eq == | if the value is equals to | eq;\<value to compare> | | ne != <> | if the value is not equals to | nq;\<value to compare> | | null | The value must be null. It is different to nullable because nullable allow other conditions | null | | empty | if the value is empty | empty | | notnull | if the value is not null | notnull | | lt | if the value is less than | lt;\<value to compare> | | lte | if the value is less or equals than | lte;\<value to compare> | | gt | if the value is great than | gt;\<value to compare> | | gte | if the value is great or equals than | gte;\<value to compare> | | between | if the value is between | between;\<from,to> | | true | if the value is true or 1 | true | | false | if the value is false, or 0 | false | | array | if the value is an array | array | | int | if the value is an integer | int | | string | if the value is a string | string | | float | if the value is a float | float | | object | if the value is an object | object | | in | the value must be in a list | in;apple,pear,banana | | notin | the value must not be in a list | notin;apple,pear,banana |
Returns the whole array transformed. If you want the current navigation then use current()
Example:
$this->set($array)->nav('field')->all();
Returns the result indicated by nav(). If you want to return the whole array, then use all()
Example:
$this->set($array)->nav('field')->current();
Copyright Jorge Castro Castillo 2023. 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.
Classes of Jorge Castro | > | PHP Array One | > | Download .zip .tar.gz | > | Support forum | > | Blog | > | Latest changes |
|
Groups | Applications | Files |
Groups |
PHP 5 | Classes using PHP 5 specific features | View top rated classes |
Data types | Modeling and manipulating data types | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
examples (2 files) | ||||
src (1 file) | ||||
test (2 files) | ||||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | examples |
File | Role | Description |
---|---|---|
ex1.php | Example | Example script |
example2.php | Example | Example script |
Files | / | test |
File | Role | Description |
---|---|---|
ArrayOneTest.php | Class | Class source |
bootstrap.php | Aux. | Auxiliary script |
Download all files: arrayone.tar.gz arrayone.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
examples (2 files) | ||||
src (1 file) | ||||
test (2 files) | ||||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | examples |
File | Role | Description |
---|---|---|
ex1.php | Example | Example script |
example2.php | Example | Example script |
Files | / | test |
File | Role | Description |
---|---|---|
ArrayOneTest.php | Class | Class source |
bootstrap.php | Aux. | Auxiliary script |
Download all files: arrayone.tar.gz arrayone.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|