mgd_group_id_array()

Ever had the problem that you required a sorting result be grouped by some other field. For example a mgd_get_topic_articles_all grouped by topic and sorted in some courius way. This function is to solve this problem. First it groups all Records together after a specified group key, then it sorts both the groups themselves and the elements in the group.

Synopsis

This function takes an ID-Array of Objects, groups it by it given key. Then both the Groups themself and the Elements in the groups are sorted by a given key. The result is an associative array which containes the group id as a key and an array containing the element IDs as a value.

Calling Syntax

mgd_group_id_array ($array, $groupkey,
$element_type, $group_type,
$elementsortkey = "unsorted",
$groupsortkey = "unsorted",
$elementsorting = SORT_REGULAR,
$groupsorting = SORT_REGULAR )

Parameter Syntax

$array: This is the ID-Array, that has to be grouped and ordered.

$groupkey: The field, which is used to group the IDs.

$element_type, $group_type: The Names of the Objects to be grouped. $element_type is the class name of the Objects in $array, $group_type is the class name of the object type, the $groupkey refers to. Both are needed for sorting. See mgd_sort_id_array() for further information about this parameters.

$elementsortkey, $groupsortkey: These are the fields of the element classes and group classes, after which should be sorted, again referring to the Elements and the Groups, as above. Since mgd_sort_id_array() is used internally to do the sorting, see there. By default, no sorting is done.

$elementsorting, $groupsorting: This is the sort order, according to PHPs sort-function.

Return Value

This function will return an assiociative Array whose Keys are the found Group-IDs and whose Elements are the assiociated Elements to that group. It looks roughly like this:

array (
[Group ID 1] => array([Element 1], [Element 2] ...),
[Group ID 2] => array([Element 1], [Element 2] ...),
...);

Note, that this Array is not neccesserily symetric, like other 2-Dimensional Arrays, though the same syntax can be used to access it.

Hints

This function is powerful if you need to display small hierarchies. The support function mgd_sort_group_id_array() can be used to resort the same array to different views.

The probable best way to traverse this array is a nested foreach-construct, one for the group array, one for the element subarrays.

Dependencies

Code

<?php

function mgd_group_id_array ($array, $groupkey,
  
$element_type, $group_type,
  
$elementsortkey = "unsorted", $groupsortkey = "unsorted",
  
$elementsorting = SORT_REGULAR,
  
$groupsorting = SORT_REGULAR ) {
  
  
$element_createstr = mgd_get_createstr($element_type);
  
  
// 1. Group together by groupkeys
  
  
$result = array();
  
$found_keys = array();
  
  foreach (
$array as $element)
  {
    eval(
"\$tmp = $element_createstr($element);");
    if (
in_array($tmp->$groupkey, $found_keys))
    {
      
$result[$tmp->$groupkey][] = $tmp->id;
    } else {
      
$found_keys[] = $tmp->$groupkey;
      
$result[$tmp->$groupkey] = array($tmp->id);
    }
  }
  
  
// 2. Now sort it
  
  
mgd_sort_group_id_array($result, $element_type, $group_type,
    
$elementsortkey, $groupsortkey, $elementsorting,
    
$groupsorting);

  return
$result;  
}

?>