Group - maximiliamus/weby-sloth GitHub Wiki

Operation also known as "group by".

Separates data into groups, which can be aggregated with provided functions.

How to list groups

$sloth = Sloth::from($data)
    ->group('foo');
$sloth->print();
foo
one
two
print_r($sloth->fetch());
Array
(
    [0] => Array
    (
        [foo] => one
    )
    [1] => Array
    (
        [foo] => two
    )
)

How to count group rows

$sloth = Sloth::from($data)
    ->group('foo')
    ->count('*');
$sloth->print();
foo     *
one     4
two     5
print_r($sloth->fetch());
Array
(
    [0] => Array
    (
        [foo] => one
        [*] => 4
    )
    [1] => Array
    (
        [foo] => two
        [*] => 5
    )
)

How to count column values

$sloth = Sloth::from($data)
    ->group('foo', 'baz')
    ->count();
$sloth->print();
foo     baz
one     3
two     4
print_r($sloth->fetch());
Array
(
    [0] => Array
    (
        [foo] => one
        [baz] => 3
    )
    [1] => Array
    (
        [foo] => two
        [baz] => 4
    )
)
⚠️ **GitHub.com Fallback** ⚠️