note 90917 added to function.array-multisort

From: Date: Sat, 16 May 2009 10:45:22 +0000
Subject: note 90917 added to function.array-multisort
Groups: php.notes 
Request: Send a blank email to php-notes+get-154656@lists.php.net to get a copy of this message
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 /* Example structure */ 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); /* Source */ 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"); } ?> ---- Server IP: 91.199.167.18 Probable Submitter: 90.141.9.178 ---- Manual Page -- http://www.php.net/manual/en/function.array-multisort.php Edit -- https://master.php.net/note/edit/90917 Del: integrated -- https://master.php.net/note/delete/90917/integrated Del: useless -- https://master.php.net/note/delete/90917/useless Del: bad code -- https://master.php.net/note/delete/90917/bad+code Del: spam -- https://master.php.net/note/delete/90917/spam Del: non-english -- https://master.php.net/note/delete/90917/non-english Del: in docs -- https://master.php.net/note/delete/90917/in+docs Del: other reasons-- https://master.php.net/note/delete/90917 Reject -- https://master.php.net/note/reject/90917 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154656) next »