PHPackages                             8ctopus/stats-table - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. 8ctopus/stats-table

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

8ctopus/stats-table
===================

Create statistics tables

1.8.0(5mo ago)032MITPHPPHP ^8.3CI passing

Since Dec 9Pushed 5mo agoCompare

[ Source](https://github.com/8ctopus/stats-table)[ Packagist](https://packagist.org/packages/8ctopus/stats-table)[ Docs](https://github.com/8ctopus/stats-table)[ RSS](/packages/8ctopus-stats-table/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (31)Used By (0)

stats-table
===========

[](#stats-table)

[![packagist](https://camo.githubusercontent.com/0323758f1430a6e10487852dde8cf4b52dd9cd087d55e4679c62690e5d804c77/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f73746174732d7461626c652f76)](https://packagist.org/packages/8ctopus/stats-table)[![downloads](https://camo.githubusercontent.com/adbc8e676bc9ef0372a82c828289305f360738c82cf70e4767b2daaeca1e80cb/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f73746174732d7461626c652f646f776e6c6f616473)](https://packagist.org/packages/8ctopus/stats-table)[![min php version](https://camo.githubusercontent.com/7fcf8a1a8ff36f5a3cd9c28c3863d20fbc5b33e2aff59a589123cd0471edf328/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f73746174732d7461626c652f726571756972652f706870)](https://packagist.org/packages/8ctopus/stats-table)[![license](https://camo.githubusercontent.com/7f6d62ffb11ba7e70684553c21d7e14de989821a981c08cae9a59bd5288ddf40/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f73746174732d7461626c652f6c6963656e7365)](https://packagist.org/packages/8ctopus/stats-table)[![tests](https://github.com/8ctopus/stats-table/actions/workflows/tests.yml/badge.svg)](https://github.com/8ctopus/stats-table/actions/workflows/tests.yml)[![code coverage badge](https://raw.githubusercontent.com/8ctopus/stats-table/image-data/coverage.svg)](https://raw.githubusercontent.com/8ctopus/stats-table/image-data/coverage.svg)[![lines of code](https://raw.githubusercontent.com/8ctopus/stats-table/image-data/lines.svg)](https://raw.githubusercontent.com/8ctopus/stats-table/image-data/lines.svg)

Create statistics tables and export them to text, JSON, CSV or Excel.

introduction
------------

[](#introduction)

This package facilitates the creation of statistical tables from datasets. It provides features including data aggregation (sum, count, average), dynamic column calculations, data grouping and sorting. The generated tables can be exported to text, JSON, CSV, or Excel.

This package is a fork of [paxal/stats-table](https://github.com/paxal/stats-table). Migrating from the parent package shouldn't be too hard, but except a bit of work.

installation
------------

[](#installation)

```
composer require 8ctopus/stats-table

```

*NOTE*: This package uses `array_all` which exists only in php 8.4, if you are not using it yet, you must require `symfony/polyfill-php84` package.

usage
-----

[](#usage)

The `StatsTableBuilder` class helps combine data from multiple tables, build aggregations (column sum, count, average, ...), create calculated columns, and add grouping. While the second class `StatsTable` allows to sort the table and remove columns.

examples
--------

[](#examples)

Play with the examples in `demo.php`.

### example 1

[](#example-1)

First example shows aggregation, dynamic column and table sorting.

```
$data = [
    [
        'name' => 'Pierre',
        'age' => 32,
        'weight' => 100,
        'height' => 1.87,
    ], [
        'name' => 'Jacques',
        'age' => 28,
        'weight' => 60,
        'height' => 1.67,
    ], [
        'name' => 'Jean',
        'age' => 32,
        'weight' => 80,
        'height' => 1.98,
    ], [
        'name' => 'Paul',
        'age' => 25,
        'weight' => 75,
        'height' => 1.82,
    ],
];

$headers = [
    'name' => 'Name',
    'age' => 'Age',
    'weight' => 'Weight',
    'height' => 'Height',
];

$formats = [
    'name' => Format::String,
    'age' => Format::Integer,
    'weight' => Format::Float,
    'height' => Format::Float,
];

$aggregations = [
    'name' => new CountAggregation('name', Format::Integer),
    'age' => new AverageAggregation('age', Format::Integer),
    'weight' => new AverageAggregation('weight', Format::Integer),
    'height' => new AverageAggregation('height', Format::Float),
];

$builder = new StatsTableBuilder($data, $headers, $formats, $aggregations);

// add body mass index row to table
$dynamicColumn = new CallbackColumnBuilder(function (array $row) : float {
    return $row['weight'] / ($row['height'] * $row['height']);
});

$table = $builder
    ->addDynamicColumn('BMI', $dynamicColumn, 'BMI', Format::Float, new AverageAggregation('BMI', Format::Float))
    ->build();

$table->sortByColumns([
    'age' => Direction::Ascending,
    'height' => Direction::Ascending,
]);

$dumper = new TextDumper();
echo $dumper->dump($table);
```

```
     Name  Age  Weight  Height    BMI
     Paul   25   75.00    1.82  22.64
  Jacques   28   60.00    1.67  21.51
   Pierre   32  100.00    1.87  28.60
     Jean   32   80.00    1.98  20.41
        4   29      78    1.84  23.29
```

### example 2

[](#example-2)

Here's another example with a dynamic column which depends on the aggregation result. We add a column that calculates the percentage of each status. This is useful, for example, when your data comes from a database request, as it drastically simplifies the of the database query.

```
$data = [
    [
        'status' => 'active',
        'count' => 80,
    ], [
        'status' => 'cancelled',
        'count' => 20,
    ],
];

$headers = [];

$formats = [
    'status' => Format::String,
    'count' => Format::Integer,
];

$aggregations = [
    'count' => new SumAggregation('count', Format::Integer),
];

$builder = new StatsTableBuilder($data, $headers, $formats, $aggregations);

// get count column total
$total = $aggregations['count']->aggregate($builder);

// add percentage column
$dynamicColumn = new CallbackColumnBuilder(function (array $row) use ($total) : float {
    return $row['count'] / $total;
});

$table = $builder
    ->addDynamicColumn('percentage', $dynamicColumn, 'percentage', Format::Percent, new SumAggregation('percentage', Format::Percent))
    ->build();

echo (new TextDumper())
    ->dump($table);
```

```
     status  count  percentage
     active     80         80%
  cancelled     20         20%
               100        100%
```

### example 3

[](#example-3)

The third example demonstrates a dynamic column containing consolidated revenue and group by date.

```
