|
Posted by Janwillem Borleffs on 05/21/06 15:43
Paul Lautman wrote:
> Can you give me an example of where usort and uasort give different
> outputs and how uksort would be used?
>
function cmp($a, $b) {
if ($a == $b) return 0;
return $a < $b ? -1 : 1;
}
$array = array('b' => 200, 'a' => 50, 'A' => 100);
$usort = $uasort = $uksort = $array;
print 'original array:<br>';
print_r($array);
print '<hr>usort: sorted by value, keys are not preserved:<br>';
usort($usort, 'cmp');
print_r($usort);
print '<hr>uasort: sorted by value, keys are preserved:<br>';
uasort($uasort, 'cmp');
print_r($uasort);
print '<hr>uksort: sorted by key;<br>';
print '"A" put before "a" because of its lower ASCII value:<br>';
uksort($uksort, 'cmp');
print_r($uksort);
JW
Navigation:
[Reply to this message]
|