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

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* 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: uksort
View the source code for this pageSearch the site



Previous page
 uasort
 Updated
Sat, 12 Aug 2000
usort 
Next page


uksort

(PHP3 >= 3.0.4, PHP4 )

uksort --  Sort an array by keys using a user-defined comparison function

Description

void uksort (array array, function cmp_function)

This function will sort the keys of an array 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.

Example 1. Uksort() example


function cmp ($a, $b) {   
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
}

$a = array (4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");

uksort ($a, "cmp");

while (list ($key, $value) = each ($a)) {
    echo "$key: $value\n";
}
      

This example would display:


20: twenty
10: ten
4: four
3: three
      

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


User Contributed Notes: uksort


antosch@gmx.de
20-Feb-2000 08:21
this example filters double numbers, that means if u have two times 30, it just displays it one time in the result. any clues how this is prevented?


Jesse@bend.com
26-Jul-2000 05:02
Filters double numbers you say?
Assuming you mean an array like <?=array( 1 => "one", 2 => "two", 1 => "one")?> loses the redundant keys, I have to say that I was under the impression that assosciative arrays required unique keys to begin with. Otherwise are you trying to say that you have non-unique keys that are getting lost?



 About Notes


Previous page
 uasort
 Updated
Sat, 12 Aug 2000
usort 
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.