Here is useful example based on za at byza dot it solution to sort multidimensional objects by any dimension.
za at byza dot it
<?php
class person{
function __construct($firstName, $lastName, $title, $position){
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->title = new title($title);
$this->position = new position($position);
}
}
class title{
function __construct($name){
$this->name = $name;
}
}
class position{
function __construct($name){
$this->name = $name;
}
}
$array[] = new person('Piotr', 'Sobiepanek', 'b', 'b');
$array[] = new person('Piotr', 'Kowalski', 'b', 'a');
$array[] = new person('Piotr', 'Michalski', 'a', 'a');
$array[] = new person('Jozef', 'Smietana', 'a', 'b');
$array[] = new person('Jozef', 'Cmietana', 'a', 'b');
$array[] = new person('Marcin', 'Kondraciuk', 'c', 'b');
$array[] = new person('Maksym', 'Kondraciuk', 'c', 'd');
$array[] = new person('Ambrozy', 'Kondraciuk', 'c', 'd');
$array[] = new person('Alojzy', 'Kondraciuk', 'c', 'd');
array_sort($array, 'title->name', 'position->name', 'lastName');
print_r($array);
function hod(&$base, $path){
$keys = explode("->", $path);
$keys[0] = str_replace('$', '', $keys[0]);
$expression = '$ret = ';
$expression.= '$';
foreach ($keys as $key){
if (++$licz == 1){
$expression.= 'base->';
} else {
$expression.= $key.'->';
}
}
$expression = substr($expression, 0, -2);
$expression.= ';';
eval($expression);
return $ret;
}
function array_sort_func($a,$b=NULL) {
static $keys;
if($b===NULL) return $keys=$a;
foreach($keys as $k) {
if($k[0]=='!') {
$k=substr($k,1);
if(hod($a, '$a->'.$k)!==hod($b, '$b->'.$k)) {
return strcmp(hod($b, '$b->'.$k),hod($a, '$a->'.$k));
}
}
else if(hod($a, '$a->'.$k)!==hod($b, '$b->'.$k)) {
return strcmp(hod($a, '$a->'.$k),hod($b, '$b->'.$k));
}
}
return 0;
}
function array_sort(&$array) {
if(!$array) return $keys;
$keys=func_get_args();
array_shift($keys);
array_sort_func($keys);
usort($array,"array_sort_func");
}
?>