rpkamp/report-array
Composer 安装命令:
composer require rpkamp/report-array
包简介
Easily create aggregate arrays from flat data
关键字:
README 文档
README
Easily create aggregate arrays from flat data.
Creating a structured array from flat data that comes from a relational database for example can be quite a hassle.
Suppose you have data like in the following table:
| product | country | count |
|---|---|---|
| foo | NL | 5 |
| foo | US | 10 |
| bar | UK | 3 |
| bar | DE | 2 |
| bar | DE | 8 |
And you want to transform it to an array like the following:
$report = [ 'foo' => [ 'NL' => 5, 'US' => 10, ], 'bar' => [ 'UK' => 3, 'DE' => 10, ] ];
What you often end up with is code that looks like this:
$report = []; foreach ($rows as $row) { if (!isset($report[$row['product']][$row['country']])) { $report[$row['product']][$row['country']] = 0; } $report[$row['product']][$row['country']] += $row['count']; }
The isset part takes up 3 lines total and is hard to read.
This is where ReportArray comes in. Instead of the above you can do the following:
$storage = new rpkamp\ReportArray\Storage(); $report = new rpkamp\ReportArray\ReportArray($storage); foreach ($rows as $row) { $report->add($row['product'], $row['country'], $row['count']); }
At any point if you want the array shown above just call $report->get().
That's it. No more isset, just tell the class to add a value and it will assume a value of 0 for any key that was not yet set.
If you want a different value than 0 as default value, pass it to the rpkamp\ReportArray\Storage class constructor as an argument.
In addition to the add method, there is also sub for subtraction, mul for multiplication, div for division, pow for powers and root for roots.
Installation
From the command line run:
composer require rpkamp/report-array
Adding methods
You can easily add your own method if you need to by providing it to rpkamp\ReportArray\ReportArray#addMethod.
For example:
$storage = new rpkamp\ReportArray\Storage(); $report = new rpkamp\ReportArray\ReportArray($storage); $report->addMethod('myCustomMethod', function ($carry, $value) { return 2 * $carry + $value; }); $report->set('foo', 2); $report->myCustomMethod('foo', 10); $report->get(); // returns ['foo' => 14] (2 * 2 + 10)
rpkamp/report-array 适用场景与选型建议
rpkamp/report-array 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.72k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「reporting」 「aggregate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rpkamp/report-array 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rpkamp/report-array 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rpkamp/report-array 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Reportico Open Source PHP Report Designer
Monolog JSON Formatter for Google StackDriver integration
Provides a collection of entities, helpers and base implementations for creating domain objects.
Trait providing methods to set class properties with an array.
Result set aggregation class.
统计信息
- 总下载量: 11.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-13