PHP Classes

File: readme.txt

Recommend this page to a friend!
  Classes of Willy Svenningsson  >  PHP Group By Array  >  readme.txt  >  Download  
File: readme.txt
Role: Documentation
Content type: text/plain
Description: Readme
Class: PHP Group By Array
Group bidimensional arrays by a given element
Author: By
Last change: Prettyfied
Date: 6 years ago
Size: 1,463 bytes
 

Contents

Class file image Download
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])
For more information send a message to info at phpclasses dot org.