francerz / php-power-data
PHP data structures for complex data handling
Installs: 1 384
Dependents: 9
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
- dev-master
- v0.1.37
- v0.1.36
- v0.1.35
- v0.1.34
- v0.1.33
- v0.1.32
- v0.1.31
- v0.1.30
- v0.1.29
- v0.1.28
- v0.1.27
- v0.1.26
- v0.1.25
- v0.1.24
- v0.1.23
- v0.1.22
- v0.1.21
- v0.1.20
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-12-06 16:52:15 UTC
README
Installation
This package can be installed with composer using following command.
composer require francerz/php-power-data
Using Index
class
use Francerz\PowerData\Index; $index = new Index($data, ['column1', 'column2']); $col1Values = $index->getColumnValues('column1'); $col2Values = $index->getColumnValues('column2'); foreach ($col1Values as $c1) { foreach ($col2Values as $c2) { // Retrieves all items that matches $c1 and $c2. $items = $index[['column1' => $c1, 'column2' => $c2]]; $numItems = $index->xCount(['column1' => $c1, 'column2' => $c2]); $sumCol3 = $index->sum('column3', ['column1' => $c1, 'column2' => $c2]); $first = $index->first(['column1' => $c1, 'column2' => $c2]); $last = $index->last(['column1' => $c1, 'column2' => $c2]); } // Retrieves all items that matches $c1 $items = $index[['column1' => $c1]]; }
Method groupBy($columns)
The groupBy
method allows you to group records from an indexed dataset based
on one or more column values. The grouping is performed incrementally, meaning
that the method applies filters progressively on the existing index to avoid
unnecesary memory and processing overhead. This ensures that the performance
remains efficient, even with large datasets.
Parameters:
$columns
(string|array): The name of a single column (string) or an array of column names (array) by which the data should be grouped. The method will return a nested grouping if multiple columns are provided.
Returns:
- array: Returns a nested associative array, where each key corresponds to the unique values in the specified columns. The innermost arrays contain the records that match teh specific grouping of column values.
How it works:
The methods does not create a new index fore each group. Instead, progressively filters the data of each column's unique values, grouping the results step by step. This approach reduces memory consumption and processing time by avoiding redundant operations on the dataset.
Example Usage:
$index = new Index($data, ['country', 'city']); // Group by a single column $groupedByCity = $index->groupBy('city'); // Group by multiple columns $groupedByCountryAndCity = $index->groupBy(['country', 'city']); print_r($groupedByCity);
Aggregations
Aggregations::concat(array $values, $separator = '') Aggregations::count(array $values, bool $ignoreNulls = false) Aggregations::findPercentile(array $values, float $value, int $flags = self::PERCENTILE_FLAGS_MIDDLE) Aggregations::first(array $values) Aggregations::frequencies(array $values) Aggregations::last(array $values) Aggregations::max(array $values) Aggregations::mean(array $values, bool $ignoreNulls = false) Aggregations::median(array $values) Aggregations::min(array $values) Aggregations::mode(array $values, bool $strict = false) Aggregations::percentile(array $values, float $percentile, int $flags = self::PERCENTILE_FLAGS_MIDDLE) Aggregations::product(array $values, bool $ignoreEmpty = false) Aggregations::sum(array $values)
Arrays
Arrays::hasNumericKeys(array $array) Arrays::hasStringKeys(array $array) Arrays::findKeys(array $array, string $pattern) Arrays::remove(array &$array, $value) Arrays::filter($array, $callback = null, $flag = 0) Arrays::intersect(array $array1, array $array2, ...$_)