GroupBy class - Generate hierarchical array on resultsets or similar
using the SQL GROUP BY metaphor
method: groupBy
usage: groupBy($rows , $keyColsIndex)
params:
$rows = an array of arrays of columns like a resultset from SQL
$keyColsIndex = an array of column index specifing the key. [0, nrcols-1]
how to use the output:
[$res = (new GroupBy())->groupBy($rows, $keyColsIndex);]
foreach ($res as $aGroup)
{
$groupKey = $aGroup[0];
$groupRows = $aGroup[1];
// How many lines is given by count($groupRows)
foreach ($groupRows as $groupRow)
{
// We got all columns in $groupRow
// (And the key cols in $groupKey)
// (And you can GroupBy on the $groupRow to get even more hierarchy)
}
// Here we can do any SQL aggregate functions on '$groupRows' like COUNT, MAX, MIN etc
}
This is the only method in the class.
returns:
A sorted array of arrays.
Each item has an array for the 'grouby' values
and an array with arrays of column values for this group
[
[ [groubyols,], [ [restcols,],...] ],
]
like
[
[ ["A",1], [ ["23","14"],["19",4"] ] ],
[ ["B",2], [ ["22","54"],["35","41"],["0","99] ] ]
]
for the (unsorted!) input
[
["A","1","23","14"],
["B","2","22","54"],
["B","2","35","41"],
["A","1","19","4"],
["B","2","0","99"]
]
and call ->groupBy(data, [0, 1])
|