2017 © Pedro Peláez
 

library report-array

Easily create aggregate arrays from flat data

image

rpkamp/report-array

Easily create aggregate arrays from flat data

  • Thursday, October 12, 2017
  • by ScallioXTX
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6,176 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 9 % Grown

The README.md

report-array

Easily create aggregate arrays from flat data., (*1)

Creating a structured array from flat data that comes from a relational database for example can be quite a hassle., (*2)

Suppose you have data like in the following table:, (*3)

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:, (*4)

$report = [
    'foo' => [
        'NL' => 5,
        'US' => 10,
    ],
    'bar' => [
        'UK' => 3,
        'DE' => 10,
    ]
];

What you often end up with is code that looks like this:, (*5)

$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:, (*6)

$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()., (*7)

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., (*8)

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., (*9)

Installation

From the command line run:, (*10)

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., (*11)

For example:, (*12)

$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)

The Versions

12/10 2017

dev-master

9999999-dev http://github.com/rpkamp/report-array

Easily create aggregate arrays from flat data

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

array reporting aggregate

08/10 2017

0.2

0.2.0.0 http://github.com/rpkamp/report-array

Easily create aggregate arrays from flat data

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

array reporting aggregate

09/04 2016

0.1

0.1.0.0 http://github.com/rpkamp/report-array

Easily create aggregate arrays from flat data

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

array reporting aggregate