note 52888 added to function.uasort

From: Date: Tue, 17 May 2005 00:36:23 +0000
Subject: note 52888 added to function.uasort
Groups: php.notes 
Request: Send a blank email to php-notes+get-89510@lists.php.net to get a copy of this message
A subtle bug, corrected... <?php function masort(&$data, $sortby) { static $sort_funcs = array(); if (empty($sort_funcs[$sortby])) { $code = "\$c=0;"; foreach (split(',', $sortby) as $key) { $array = array_pop($data); array_push($data, $array); if(is_numeric($array[$key])) $code .= "if ( \$c = ((\$a['$key'] == \$b['$key']) ? 0:((\$a['$key'] < \$b['$key']) ? -1 : 1 )) ) return \$c;"; else $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return \$c;\n"; } $code .= 'return $c;'; $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code); } else { $sort_func = $sort_funcs[$sortby]; } $sort_func = $sort_funcs[$sortby]; uasort($data, $sort_func); } ?> Note that $code .= "if ( \$c = ((\$a['$key'] == \$b['$key']) ? 0:((\$a['$key'] < \$b['$key']) ? -1 : 1 )) ) return \$c;"; Has had the "return \$c" added. Ultimately what the method is trying to accomplish is to build a chain of sort-order precedence. But this requires each evaluation to short-circuit out with a return. It was missing. Peace ---- Manual Page -- http://www.php.net/manual/en/function.uasort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52888 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52888&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52888&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52888&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52888&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52888&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52888&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89510) next »