PHP Classes
elePHPant
Icontem

PHP Group By Array: Group bidimensional arrays by a given element

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2015-09-04 (2 years ago) RSS 2.0 feedNot enough user ratingsTotal: 169 All time: 8,365 This week: 701Up
Version License PHP version Categories
group-by 1.0GNU General Publi...5.4PHP 5, Data types
Description Author

This class can broup bidimensional arrays by a given element.

It can take as parameter an array that contains a set of arrays with element values.

The class sorts the array grouping it by the elements of the set of arrays in a given positions similar to SQL GROUP BY clause.

Multiple elements may be used to group the array.

The class returns an array with the grouped elements containing two values: the first with the grouping key values and another with an array with remaining values.

Innovation Award
PHP Programming Innovation award nominee
September 2015
Number 11
GROUP BY is a SQL clause that allows grouping query results by values of certain result columns.

This package implements the group by SQL clause logic but applies it to bidimensional arrays.


Manuel Lemos
  Performance   Level  
Name: Willy Svenningsson <contact>
Classes: 1 package by
Country: Sweden Sweden
Age: 70
All time rank: 399926 in Sweden Sweden
Week rank: 881 Up5 in Sweden Sweden Up
Innovation award
Innovation award
Nominee: 1x

Details
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])
  Files folder image Files  
File Role Description
Plain text file GroupBy.class.php Class The very class
Accessible without login Plain text file index.php Example Test
Accessible without login Plain text file readme.txt Doc. Readme

 Version Control Unique User Downloads Download Rankings  
 0%
Total:169
This week:0
All time:8,365
This week:701Up