note 94411 added to function.usort

From: Date: Tue, 03 Nov 2009 15:40:39 +0000
Subject: note 94411 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-162357@lists.php.net to get a copy of this message
That's a port of Will Shaver's better implementation for to versions of PHP. Note that string comparison is a bit poor though... function osort(&$array, $props) { if(!is_array($props)) $props = array($props => true); $funcBody = ''; foreach($props as $prop => $ascending) { $funcBody .= 'if($a->'.$prop.' != $b->'.$prop.'){'; if ($ascending) $funcBody .= 'return $a->'.$prop.' > $b->'.$prop.' ? 1 : -1;'; else $funcBody .= 'return $b->'.$prop.' > $a->'.$prop.' ? 1 : -1;'; $funcBody .= '}'; } $funcBody .= 'return -1;'; usort($array, create_function ('$a, $b', $funcBody)); } ---- Server IP: 69.147.83.197 Probable Submitter: 194.116.240.25 ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/94411 Del: integrated -- https://master.php.net/note/delete/94411/integrated Del: useless -- https://master.php.net/note/delete/94411/useless Del: bad code -- https://master.php.net/note/delete/94411/bad+code Del: spam -- https://master.php.net/note/delete/94411/spam Del: non-english -- https://master.php.net/note/delete/94411/non-english Del: in docs -- https://master.php.net/note/delete/94411/in+docs Del: other reasons-- https://master.php.net/note/delete/94411 Reject -- https://master.php.net/note/reject/94411 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162357) next »