uksortSORT an ARRAY by keys using an
user-defined comparison function.
This function returns TRUE on success or FALSE on failure.
This function will sort an array by its keys using a user-supplied comparison function.
The $value_compare_func, comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Note that before PHP 7.0.0 this integer had to be in the range from -2147483648 to 2147483647.
<?php
bool uksort ( arr &$array , callable $value_compare_func )
where,
&$array = The input ARRAY
$value_compare_func = The comparisson function
?>
&$array
The input ARRAY.
$value_compare_func
An user-defined function.
<?php
int callback ( mix $a, mix $b )
where,
$a = First value to operate this function
$b = Second value to operate this function
?>
CAUTION
Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value.
So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.
EXERCISE
<?php
function recc01 ( $i, $j )
{
return $i%$j;
}
$arr01u = [2, 8, 18, 3, 7, 9];
echo '<pre>';
var_dump($arr01u);
echo '</pre>';
echo '<br>';
uksort($arr01u, 'recc01');
echo '<pre>';
var_dump($arr01u);
echo '</pre>';
?>
EXERCISE
<?php
function recc02 ( $i, $j )
{
return $j;
}
$arr02u = [ "Countries" => [ 'BRA' => "Brasil",
'POR' => "Portugal",
'JPN' => "Japan" ],
"Continents" => ['SA' => "South America",
'EU' => "Europe",
'AS' => "Asia"]];
echo '<pre>';
var_dump($arr02u);
echo '</pre>';
echo '<br>';
uksort($arr02u, 'recc02');
echo '<pre>';
var_dump($arr02u);
echo '</pre>';
?>
EXERCISE
<?php
function recc03($i, $j)
{
return strcmp($i[0], $j[0]);
}
$colors[0][0] = "RED";
$colors[1][0] = "GREEN";
$colors[2][0] = "BLUE";
uksort($colors, "recc03");
echo '<pre>';
var_dump($colors);
echo '</pre>';
$c = count($colors) - 1;
for($x = 0; $x <= $c; $x++)
{
echo $x . ' => ' . $colors[$x][0] . '<br>';
}
?>
EXERCISE
<?php
function recc04($i, $j)
{
$rnd04 = mt_rand(0,5);
if ($i + $j == $rnd04)
{
echo 'j = ' . $j . ' rand = ' . $rnd04 . '<br>';
return $j;
}
else
{
echo 'i = ' . $i . ' rand = ' . $rnd04 . '<br>';
return $i;
}
}
$arr04s = [ "Apple",
"Strawberry",
"Pineapple",
"Banana",
"Orange" ];
foreach($arr04s as $k04s => $v04s);
echo $k04s . ' => ' . $v04s . '<br><br>';
if(uksort($arr04s, 'recc04') == true)
{
foreach($arr04s as $k04s => $v04s);
echo '<br>' . $k04s . ' => ' . $v04s . '<br>';
}
?>
EXERCISE
<?php
/*
* Function is implemented in
* ext/standard/array.c
*/
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
uasort($a, "cmp");
foreach($a as $key => $value) {
echo "original key = $key => value = $value<br>";
}
?>
EXERCISE
<?php
/* Sort the entries by key
* using user defined function.
* Source code: ext/spl/spl_array.c
* Alias to functions:
*/
echo "Testing ArrayObject::uksort() :
basic functionality.";
// Reverse sorter
function cmp($value1, $value2) {
if($value1 == $value2) {
return 0;
}
else if($value1 < $value2) {
return 1;
}
else
return -1;
}
$ao = new ArrayObject([3=>0, 2=>1, 5=>2, 6=>3, 1=>4 ]);
$ao->uksort('cmp');
echo '<pre>';
var_dump($ao);
echo '</pre>';
?>
EXERCISE
<?php
/* Sort the entries by key
* using user defined function.
* Source code: ext/spl/spl_array.c
* Alias to functions:
*/
$ao = new ArrayObject();
try {
$ao->uksort();
} catch (ArgumentCountError $e) {
echo $e->getMessage() . "<br>";
}
try {
$ao->uksort(1,2);
} catch (ArgumentCountError $e) {
echo $e->getMessage() . "<br>";
}
?>