Skip to content
Maxim Stupakov edited this page May 7, 2018 · 14 revisions

The output data of Sloth's operation depends on type of the input data. This is the map:

Input data Output data
Array of indexed arrays Array of indexed arrays
Array of associative arrays Array of associative arrays
Array of objects Array of ArrayObjects

By analogy with Input, for simplicity, we will refer to these output arrays as:

  • the indexed output;
  • the associative output;
  • the object output.

Please note, that indexed and associative arrays are the same in PHP, and here they are just а roughly categorized.

Indexed Output

$data = [
    ['one', 'A',  1],
    ['one', null, null],
    ['one', 'B',  2],
    ['one', 'C',  3],
    ['two', 'A',  4],
    ['two', null, null],
    ['two', 'B',  5],
    ['two', 'C',  6],
    ['two', 'D',  7],
];

$result = Sloth::from($data)
    ->group(0, [1, 2])
    ->first(1)
    ->count(2)
    ->fetch();

print_r($result);

// Outputs:
// Array
// (
//     [0] => Array
//         (
//             [0] => one
//             [1] => A
//             [2] => 3
//         )
//     [1] => Array
//         (
//             [0] => two
//             [1] => A
//             [2] => 4
//         )
// )

Associative Output

$data = [
    ['foo' => 'one', 'bar' => 'A',  'baz' => 1],
    ['foo' => 'one', 'bar' => null, 'baz' => null],
    ['foo' => 'one', 'bar' => 'B',  'baz' => 2],
    ['foo' => 'one', 'bar' => 'C',  'baz' => 3],
    ['foo' => 'two', 'bar' => 'A',  'baz' => 4],
    ['foo' => 'two', 'bar' => null, 'baz' => null],
    ['foo' => 'two', 'bar' => 'B',  'baz' => 5],
    ['foo' => 'two', 'bar' => 'C',  'baz' => 6],
    ['foo' => 'two', 'bar' => 'D',  'baz' => 7],
];

$result = Sloth::from($data)
    ->group('foo', ['bar', 'baz'])
    ->first('bar')
    ->count('baz')
    ->fetch();

print_r($result);

// Outputs:
// Array
// (
//     [0] => Array
//         (
//             [foo] => one
//             [bar] => A
//             [baz] => 3
//         )
//     [1] => Array
//         (
//             [foo] => two
//             [bar] => A
//             [baz] => 4
//         )
// )

Object Output

$data = [
    (object) ['foo' => 'one', 'bar' => 'A',  'baz' => 1],
    (object) ['foo' => 'one', 'bar' => null, 'baz' => null],
    (object) ['foo' => 'one', 'bar' => 'B',  'baz' => 2],
    (object) ['foo' => 'one', 'bar' => 'C',  'baz' => 3],
    (object) ['foo' => 'two', 'bar' => 'A',  'baz' => 4],
    (object) ['foo' => 'two', 'bar' => null, 'baz' => null],
    (object) ['foo' => 'two', 'bar' => 'B',  'baz' => 5],
    (object) ['foo' => 'two', 'bar' => 'C',  'baz' => 6],
    (object) ['foo' => 'two', 'bar' => 'D',  'baz' => 7],
];

$result = Sloth::from($data)
    ->group('foo', ['bar', 'baz'])
    ->first('bar')
    ->count('baz')
    ->fetch();

print_r($result);

// Outputs:
// Array
// (
//     [0] => ArrayObject Object
//         (
//             [storage:ArrayObject:private] => Array
//                 (
//                     [foo] => one
//                     [bar] => A
//                     [baz] => 3
//                 )
//         )
//     [1] => ArrayObject Object
//         (
//             [storage:ArrayObject:private] => Array
//                 (
//                     [foo] => two
//                     [bar] => A
//                     [baz] => 4
//                 )
//         )
// )
Clone this wiki locally