★ wanayoo — archive 1999 http://www.php.net/manual/hu/function.usort.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Tömbök
Quick Reference
English version of this pageGerman version of this pageJapanese version of this pageItalian version of this pageFrench version of this page
Tömbök
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unique
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: usort
View the source code for this pageSearch the site



Previous page
 uksort
 Updated
Sat, 12 Aug 2000
Aspell 
Next page


usort

(PHP3 >= 3.0.3, PHP4 )

usort --  Sort an array by values using a user-defined comparison function

Description

void usort (array array, string cmp_function)

This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined.

Példa 1. Usort() example


function cmp ($a, $b) {   
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
}
$a = array (3, 2, 5, 6, 1);
usort ($a, cmp);
while (list ($key, $value) = each ($a)) {
    echo "$key: $value\n";
}
      

This example would display:


0: 6
1: 5
2: 3
3: 2
4: 1
      

Megjegyzés: Obviously in this trivial case the rsort() function would be more appropriate.

Figyelem

The underlying quicksort function in some C libraries (such as on Solaris systems) may cause PHP to crash if the comparison function does not return consistent values.

See also: arsort(), asort(), ksort(), rsort(), and sort().


User Contributed Notes: usort


ccunning@math.ohio-state.edu
31-May-1999 02:34
On Solaris, usort cannot currently be used for a non-deterministic comparison function. The comparison function must return the same order for the same set of data every time. A non-deterministic comparison <b>will</b> cause the webserver to segfault. This is not a bug, but rather an effect of the Solaris libc implementation. This most common use of a non deterministic comparison function is a function that randomly returns -1 or 1 to randomly shuffle an array. PHP's shuffle function also has the same problem. To get around this, you should implement your shuffle in PHP using the function below:

<pre>
&lt;?
function myshuffle($array) {
mt_srand((double) microtime()*1000000);
$num = count($array);
for ($i=0; $i<$num-1; $i++) {
$n = mt_rand($i+1,$num);
$temp = $array[$n];
$array[$n] = $array[$i];
$array[$i] = $temp;
}
}

$array = array(1,2,3,4,5,6,7,8,9,10);
myshuffle(&$array);
while (list(,$var)=each($array)) {
echo $var . " ";
}
?>
</pre>



roads@free.fr
11-Jan-2000 08:27
Please note that you cannot use a
class member function as your comparison function for usort().

The following will not call the comparison function *at all*, leaving your array unsorted :

<PRE>
class DummyClass{
function myCmp($a,$b){
// ... compare code
}

function method($args){
// Declare your array
$myArray[]=Array(...)

// Sort the array
usort($myArray[],$this->myCmp);

// At this point, the array is
// still unsorted !
}
}
</PRE>

If you declare the function out of DummyClass, this code will work.



gfaron@integretechpub.com
28-Feb-2000 12:56
As a correction to the piece of code donated by <A HREF=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2Fhu%2F%26quot%3Bmailto%3Accunning%40math.ohio-state.edu%26quot%3B%26gt%3Bccunning%26lt%3B%2FA%26gt&y=1999 above, this function will randomize an array passed by reference. The previous version decreased the length of the array by one.

Greg
<PRE>
&lt;?php
function myshuffle($array)
{
mt_srand((double) microtime() * 1000000);
$num = count($array);
for ($i = 0; $i < $num; $i ++)
{
$n = mt_rand(0, $num - 1);
// Swap the data.
$temp = $array[$n];
$array[$n] = $array[$i];
$array[$i] = $temp;
} // ends for

} // ends function myshuffle(&array)

// Test the results.
$array = array(1,2,3,4,5,6,7,8,9,10);
myshuffle(&$array);
while (list(,$var)=each($array))
echo $var . " ";
?>



 About Notes


Previous page
 uksort
 Updated
Sat, 12 Aug 2000
Aspell 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.