nicholasmr.dk

Home    SUMS    OBBLM    NTOS    Code snippets    Contact

PHP code snippets
This section is dedicated to PHP code snippets from the OBBLM project.
I have created this section to publish some of the OBBLM code, that I feel others may be able to use in their projects, but is hidden away in the OBBLM repository.

objsort()

<?php

// Sorts array of objects by common object properties. 
// Usage: objsort($obj_array, array('+X', '-Y')) ...to sort objects by X ascending followed by Y descending.
function objsort(&$obj_array$fields)
{
    
$idxs count($fields)-1;   # Number of fields to sort by.
    
$func 'return ';          # Anonymous function used for sorting the object array.
    
$parens 0;                # Number of parentheses added to end of anonymous function.
    
    
for ($i 0$i <= $idxs$i++) {
        
$field substr($fields[$i], 1strlen($fields[$i]));
        
$sort_type substr($fields[$i], 01);
        
$parens += ($i == $idxs 2);
        
$func .= "\$a->$field " . ($sort_type == '+' '>' '<') . " \$b->$field 
                    ? 1 
                    : (\$a->
$field != \$b->$field 
                        ? -1 
                        : " 
. ($i == $idxs '0' '(');
    }

    
$func .= str_repeat(')'$parens) . ';';
    return 
usort(&$obj_arraycreate_function('$a, $b'$func));
}

?>


Tournament scheduling and management classes

See my classes at PHPclasses.org