Download the PHP package hi-folks/array without Composer
On this page you can find all versions of the php package hi-folks/array. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hi-folks/array
More information about hi-folks/array
Files in hi-folks/array
Package array
Short Description Arr class for managing arrays, and Table class for managing bi-dimensional arrays, built on top of the PHP array functions.
License MIT
Homepage https://github.com/hi-folks/array
Informations about the package array
Package array
This package provides 2 classes:
- Arr class is built on top of the PHP array functions.
- Table class allow you to manage bidimensional associative array (like a table or tuple).
Arr class
Arr exposes methods to create, manage, access the data structure of the array.
The interface (method names, method arguments) are pretty similar to the Javascript Array class.
I built this class because comparing method functions arrays of Javascript and PHP i think (my personal thought) that the JS one is smoother and has a good developer experience (but, again, it's a personal opinion).
The Arr class provides some methods:
- make() create array;
- fromFunction(): create Arr from a function;
- fromValue(): create Arr from a value;
- length(): length/size of the array;
- arr(): returns data with the type PHP array
- get(): get the element by index
- getArr(): get the element (as Arr::class instance) by index (helpful for nested arrays)
- Iterator methods: current(), next(), prev(), key(), valid(), rewind()
- forEach(): execute a function for each element;
- push(): add new element (at the end);
- pop(): remove an element (at the end);
- unshift(): add new element at the start;
- shift(): remove an element from the start;
- append(): append arrays to the current one;
- concat(): return new array joining more arrays, Arr objects or scalar variables;
- join(): joins all elements into a string;
- slice(): returns a sub array;
- indexOf(): find the first occurrence;
- lastIndexOf(): find the last occurrence;
- every(): all elements match a fn();
- some(): at least one element matches a fn();
- filter(): filter elements by a fn();
- map(): apply a fn() for each element;
- flat(): flat an array of arrays;
- flatMap(): map() and flat();
- fill(): fill an array (or a piece of an array);
- reduce(): calculate a fn() with the array as input;
- reduceRight(): like reduce(), but parsing the array in reverse order;
- reverse(): reverse the array;
- sort(): sort the array;
- splice(): changes content of arr removing, replacing and adding elements;
- toString(): the string representing the array (same as join(','));
- isArray(): check if the input is an array;
- from(): for creating new Arr from a string or array-like object;
- findIndex(): for finding the index of some element;
- find(): returns the first element in the array that satisfies the testing function;
- entries(): returns a new Arr object that contains the key/value pairs for each index in the array;
- copyWithin(): copies part of the array to a location but keeps the original length.
- isEmpty(): checks if provided array is empty or not;
- values(): it creates a new Arr object with the values of the current one (keys are skipped)
- set(): ability to set an element to the array with a specific key
- unset(): ability to unset an element by the key
The get()
method
The get()
method supports keys/indexes with the dot (or custom) notation for retrieving values from nested arrays.
For example:
You can customize the notation with a different character:
You can define a default value in the case the key doesn't exist:
The getArr()
method
If you need to manage a complex array (nested array), or an array obtained from a complex JSON structure, you can access a portion of the array and obtain an Arr object.
Just because in the case of a complex array the get()
method could return a classic array.
Let's see an example:
The set()
method
The set()
method supports keys with the dot (or custom) notation for setting values for nested arrays.
If a key doesn't exist, the set()
method will create a new key and will set the value.
If a key already exists, the set()
method will replace the value related to the key.
For example:
So when you try to set a nested key as "content.0.content.0.text", it will be created elements as a nested array.
So if you try to dump the value of the array of $textField
you will see the following structure:
Table class
Table class allows you to manage bi-dimensional array, something like:
Each row within the Table will be of type Arr
so it allows you to lean on all the methods that
are available via the Arr
object.
Table class allows you to filter, order, select some fields, create calculated fields. The methods:
- select(): select some fields
- except(): exclude some fields
- where(): filter data
- groupBy(): grouping data
- transform(): transforms a specific field with the provided function
- orderBy(): sorting data (ascending or descending)
- toArray(): transform Table object into a native PHP array
Table
now implements \Countable
and \Iterator
, this allows you to count the number of rows
and also loop over the rows using common loops.
Installation
You can install the package via composer:
Usage
To see some examples, I suggest you to take a look to examples/cheatsheet.php file,where you can see a lot of example and use cases.
To start quickly
To create an array with random values:
You can access to the elements like a native array, but you have also Arr methods:
Usage of Table class
Starting from:
I would like to filter the rows with price greater than 100, select only "product" and "price" fields, and for each rows create a new field named "new_filed" that is a calculated field (doubling the price):
The result is
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Roberto B.
- All Contributors
License
The MIT License (MIT). Please see License File for more information.