PhpDig.net

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




uasort

Name

uasort — Sorts an array by value with a user-defined function

Synopsis

bool uasort(array, func);
array array: Array to be sorted
string func: Function name to use for sorting

Returns

TRUE on success; FALSE on failure

Description

uasort() sorts array by the value of each element, using the specified user-defined function named by func . Key/value relationships are preserved. This function is useful if the values of an associative array must be in some specific non-regular order, such as when you want to do a non-case-sensitive sort with both uppercase and lowercase alphabets.

func should be written to accept two parameters, which it must compare with each other. 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 60. Sort an array by value with a user-defined comparison function

function uasort_cmp($a, $b) { // Sort in ascending order
   if (strtolower($a) == strtolower($b)) return 0;
   return (strtolower($a) > strtolower($b)) ? 1 : -1;
}

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

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



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.