cmp.php

<?php

/**
 * @author Bjorn Jensen
 * @copyright 2011
 */

/**
 * @class cmp
 * @example code
 * 
 *  uasort($array, array(new cmp( array($key1,$key2,...,$keyN) ), "cmp__"));
 *  
 */

class cmp {
    var 
$key;
    
    function 
__construct($key) {
        
$this->key $key;
    }
    
    function 
cmp__($a,$b) {
        
$key $this->key;
        if (
$this->dive($a,$key) == $this->dive($b,$key)) return 0;
        return ((
$this->dive($a,$key) > $this->dive($b,$key)) ? : -1);
    }
    
    function 
dive($var,$keys) {
        if (
count($keys) == 1
            return 
$var[$keys[0]];
        else
            return 
$this->dive($var[array_shift($keys)],$keys);
    }
}

?>