PHP - uksort
Trieda
Metóda - uksort
(PHP 4, PHP 5, PHP 7)
The function is used to sort an array by keys using a comparison function defined by the user.
Procedurálne
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| &$array | array | The array we want to sort. | |
| $key_compare_func | callable | Specifies a callable comparison function. The function must return a negative integer if the first argument of the function is less than the second argument, a positive integer if it's greater or zero if they're equal. |
Mávratovej hodnoty
Vracia: bool
Returns true on success, returns false
otherwise.
Príklady
In the example we sort an array by its keys. The function compares the names of the keys.
// The array we want to sort:
$myArray = array("Delta" => 1, "Beta" => "100", "Epsilon" => "15.15", "Alpha" => 10, "Gamma" => 0);
// A comparison function:
function compare_key_names($key1, $key2) {
if($key1 == $key2) {
return 0;
} else {
return ($key1 < $key2) ? -1 : 1;
}
}
// Sorting the array according to our comparison function:
uksort($myArray, "compare_key_names");
foreach ($myArray as $key=>$value) {
echo "Key: $key, value: $value";
echo "<br>";
}
Súvisiace manuály
- function usort (array &$array, callable $value_compare_func) : bool
