francerz / php-power-data
此包最新版本(v0.1.37)没有可用的许可证信息。
PHP复杂数据处理数据结构
v0.1.37
2024-09-06 16:18 UTC
- 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-09-06 16:19:30 UTC
README
安装
可以使用以下命令使用composer安装此包。
composer require francerz/php-power-data
使用 Index
类
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]]; }
方法 groupBy($columns)
groupBy
方法允许您根据一个或多个列值对索引数据集中的记录进行分组。分组是逐步执行的,这意味着该方法逐步应用现有索引上的过滤器,以避免不必要的内存和处理开销。这确保了即使在大数据集上性能仍然高效。
参数
$columns
(string|array): 要按其分组的单个列名(string)或列名数组(array)。如果提供多个列,则方法将返回嵌套的分组。
返回值
- array: 返回一个嵌套关联数组,其中每个键对应于指定列中的唯一值。最内层的数组包含匹配特定列值分组的记录。
工作原理
此方法不会为每个组创建一个新的索引。相反,逐步过滤每个列的唯一值数据,逐步分组结果。这种方法通过避免在数据集上执行冗余操作来减少内存消耗和处理时间。
示例用法
$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::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::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, ...$_)