Input - maximiliamus/weby-sloth GitHub Wiki
Input data for the Sloth's operations can be:
- an array of indexed arrays;
- an array of associative arrays;
- an array of objects.
Please note, that indexed and associative arrays are the same in PHP, and here they are just а roughly categorized.
Further, for simplicity, we will refer to these input arrays as:
- the indexed input;
- the associative input;
- the object input.
Input data from this section is used for all other examples in this documentation and further are not specified.
Indexed Input
Array indexes are specified in the operation definition when the input data is the indexed input.
$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],
];
Sloth::from($data)
->group(0, [1, 2])
->first(1)
->count(2)
->print();
// Outputs:
// 0 1 2
// one A 3
// two A 4
Associative Input
Array keys are specified in the operation definition when the input data is the associative input.
$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],
];
Sloth::from($data)
->group('foo', ['bar', 'baz'])
->first('bar')
->count('baz')
->print();
// Outputs:
// foo bar baz
// one A 3
// two A 4
Object Input
Object properties are specified in the operation definition when the input data is the object input.
$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],
];
Sloth::from($data)
->group('foo', ['bar', 'baz'])
->first('bar')
->count('baz')
->print();
// Outputs:
// foo bar baz
// one A 3
// two A 4