Giter VIP home page Giter VIP logo

report-array's Introduction

report-array

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)

report-array's People

Contributors

rpkamp avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.