a::sort PHP Method

sort() static public method

Sorts a multi-dimensional array by a certain column
static public sort ( array $array, string $field, string $direction = 'desc', const $method = SORT_REGULAR ) : array
$array array The source array
$field string The name of the column
$direction string desc (descending) or asc (ascending)
$method const A PHP sort method flag.
return array The sorted array
    static function sort($array, $field, $direction = 'desc', $method = SORT_REGULAR)
    {
        $direction = strtolower($direction) == 'desc' ? SORT_DESC : SORT_ASC;
        $helper = array();
        foreach ($array as $key => $row) {
            $helper[$key] = is_object($row) ? method_exists($row, $field) ? str::lower($row->{$field}()) : str::lower($row->{$field}) : str::lower($row[$field]);
        }
        array_multisort($helper, $direction, $method, $array);
        return $array;
    }

Usage Example

Example #1
0
function tagcloud($parent, $options = array())
{
    global $site;
    // default values
    $defaults = array('limit' => false, 'field' => 'tags', 'children' => 'visible', 'baseurl' => $parent->url(), 'param' => 'tag', 'sort' => 'name', 'sortdir' => 'asc');
    // merge defaults and options
    $options = array_merge($defaults, $options);
    switch ($options['children']) {
        case 'invisible':
            $children = $parent->children()->invisible();
            break;
        case 'visible':
            $children = $parent->children()->visible();
            break;
        default:
            $children = $parent->children();
            break;
    }
    $cloud = array();
    foreach ($children as $p) {
        $tags = str::split($p->{$options}['field']());
        foreach ($tags as $t) {
            if (isset($cloud[$t])) {
                $cloud[$t]->results++;
            } else {
                $cloud[$t] = new obj(array('results' => 1, 'name' => $t, 'url' => $options['baseurl'] . '/' . $options['param'] . ':' . $t, 'isActive' => param($options['param']) == $t ? true : false));
            }
        }
    }
    $cloud = a::sort($cloud, $options['sort'], $options['sortdir']);
    if ($options['limit']) {
        $cloud = array_slice($cloud, 0, $options['limit']);
    }
    return $cloud;
}
All Usage Examples Of a::sort