A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://us2.php.net/manual/en/array.sorting.php below:

PHP: Sorting Arrays - Manual

Sorting Arrays

PHP has several functions that deal with sorting arrays, and this document exists to help sort it all out.

The main differences are:

"Matthew Rice"

12 years ago

While this may seem obvious, user-defined array sorting functions ( uksort(), uasort(), usort() ) will *not* be called if the array does not have *at least two values in it*.

The following code:

<?phpfunction usortTest($a, $b) {
var_dump($a);
var_dump($b);
return -
1;
}
$test = array('val1');
usort($test, "usortTest");$test2 = array('val2', 'val3');
usort($test2, "usortTest");?>


Will output:

string(4) "val3"


string(4) "val2"

The first array doesn't get sent to the function.

Please, under no circumstance, place any logic that modifies values, or applies non-sorting business logic in these functions as they will not always be executed.

oculiz at gmail dot com

14 years ago

Another way to do a case case-insensitive sort by key would simply be:

<?php
uksort
($array, 'strcasecmp');
?>


Since strcasecmp is already predefined in php it saves you the trouble to actually write the comparison function yourself.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4