PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




uksort

Name

uksort — Sorts the elements of an array by keys, using a user-defined function.

Synopsis

bool uksort(array);
array array: Array to sort using the supplied functionFunction with which to sort the array keys

Returns

TRUE on success; FALSE on failure

Description

uksort() sorts array based on the keys, preserving the key/value relationships. The keys are sorted using a bubblesort algorithm, which uses the user-defined comparison function named by func to evaluate whether the two elements being compared are equivalent, the first is greater than the second, or the second is greater than the first. This function can be used to modify the order in which keys are sorted, rather than using the standard order supplied with ksort() . For example, you can sort the keys in reverse order.

func should be written to accept two parameters, which it compares. If they're considered equivalent, 0 should be returned. If the first is considered to be greater, 1 should be returned. If the second is considered to be greater, -1 should be returned.

Version

PHP 3 since 3.0.4, PHP 4

Example

Example 61. Sort an array by keys using a user-defined comparison function

function uksort_cmp($a, $b) { // Sort items in descending order
   if ($a == $b) return 0;
   return ($a > $b) ? -1 : 1;
}

$arr = array("tom" => "Apple", "janet" => "apple",
             "fred" => "Banana", "barney" => "banana",
             "bullwinkle" => "Carrot", "rocky" => "carrot");
uksort($arr, "uksort_cmp");
while(list($k, $v) = each($arr)) {
   echo "$k => $v\n";
}

Output:
tom => Apple
rocky => carrot
janet => apple
fred => Banana
bullwinkle => Carrot
barney => banana



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.