note 49940 deleted from function.uasort by googleguy

From: Date: Tue, 22 Oct 2013 10:15:04 +0000
Subject: note 49940 deleted from function.uasort by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196938@lists.php.net to get a copy of this message
Note Submitter: lucas dot karisny at linuxmail dot org ---- The following is a modification of the dholme/messju masort func using david's float code with automatic data type detection. As long as the value isn't a string numeral (I.E. "1", '13.4') this should sort strings and numbers without having to explicity set which they are. <?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 )) );"; 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); } ?>

« previous php.notes (#196938) next »