uasortSORT an ARRAY using an
user-defined comparison function maintaining the index association.
This function returns TRUE on success or FALSE on failure.
This function will sort an array by its values using a user-supplied comparison function, mantaining the correlation of indexes and elements.
If two members compare as equal, their relative order in the sorted ARRAY is undefined.
This function assigns the same keys to the elements in $array.
<?php
bool uasort ( 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.
EXERCISE
<?php
function recc01 ( $i, $j )
{
return $i%$j;
}
$arr01u = [2, 8, 18, 3, 7, 9];
echo 'The given ARRAY:<br><pre>';
var_dump($arr01u);
echo '</pre>';
echo '<br>';
uasort($arr01u, 'recc01');
echo 'The SORTED ARRAY:<br><pre>';
var_dump($arr01u);
echo '</pre>';
?>
RESULT
The given ARRAY:
array(6) {
[0]=>
int(2)
[1]=>
int(8)
[2]=>
int(18)
[3]=>
int(3)
[4]=>
int(7)
[5]=>
int(9
)
The SORTED ARRAY:
array(6) {
[4]=>
int(7)
[2]=>
int(18)
[5]=>
int(9)
[3]=>
int(3)
[1]=>
int(8)
[0]=>
int(2)
)
EXERCISE
<?php
function recc02 ( $i, $j )
{
return $j%$i;
}
$arr02u = [2, 8, 18, 3, 7, 9];
echo 'The given ARRAY:<br><pre>';
var_dump($arr02u);
echo '</pre>';
echo '<br>';
uasort($arr02u, 'recc02');
echo 'The SORTED ARRAY:<br><pre>';
var_dump($arr02u);
echo '</pre>';
?>
RESULT
The given ARRAY:
array(6) {
[0]=>
int(2)
[1]=>
int(8)
[2]=>
int(18)
[3]=>
int(3)
[4]=>
int(7)
[5]=>
int(9
)
The SORTED ARRAY:
array(6) {
[4]=>
int(7)
[3]=>
int(3)
[5]=>
int(9)
[0]=>
int(2)
[2]=>
int(18)
[1]=>
int(8)
)
EXERCISE
<?php
function recc03 ( $i, $j )
{
return $j;
}
$arr03u = [ "Countries" => [ 'BRA' => "Brasil",
'POR' => "Portugal",
'JPN' => "Japan" ],
"Continents" => ['SA' => "South America",
'EU' => "Europe",
'AS' => "Asia"]];
echo 'The given ARRAY:<br><pre>';
var_dump($arr03u);
echo '</pre>';
echo '<br>';
uasort($arr03u, 'recc03');
echo 'The SORTED ARRAY:<br><pre>';
var_dump($arr03u);
echo '</pre>';
?>
RESULT
The given ARRAY:
array(2) {
["Countries"]=>
array(3) {
["BRA"]=>
string(6) "Brasil"
["POR"]=>
string(8) "Portugal"
["JPN"]=>
string(5) "Japan"
}
["Continents"]=>
array(3) {
["SA"]=>
string(13) "South America"
["EU"]=>
string(6) "Europe"
["AS"]=>
string(4) "Asia"
}
}
The SORTED ARRAY:
array(2) {
["Continents"]=>
array(3) {
["SA"]=>
string(13) "South America"
["EU"]=>
string(6) "Europe"
["AS"]=>
string(4) "Asia"
}
["Countries"]=>
array(3) {
["BRA"]=>
string(6) "Brasil"
["POR"]=>
string(8) "Portugal"
["JPN"]=>
string(5) "Japan"
}
}
EXERCISE
<?php
function recc04 ( $i, $j )
{
echo $j . '<br><br>' . $i;
}
$arr04u = [ "english" => 'unconstitutional',
"portuguese" => 'inconstitucional'];
echo 'The given ARRAY:<br><pre>';
var_dump($arr04u);
echo '</pre><br>The SORTED ARRAY:<br><br>';
uasort($arr04u, 'recc04');
?>
RESULT
The given ARRAY:
array(2) {
["english"]=>
string(16) "unconstitutional"
["portuguese"]=>
string(16) "inconstitucional"
}
The SORTED ARRAY:
inconstitucional
unconstitutional
EXERCISE
<?php
function recc05($i, $j)
{
return strcmp($i[0], $j[0]);
}
$colors[0][0] = "RED";
$colors[1][0] = "GREEN";
$colors[2][0] = "BLUE";
uasort($colors, "recc05");
echo '<pre>';
var_dump($colors);
echo '</pre>';
$c = count($colors) - 1;
for($x = 0; $x <= $c; $x++)
{
echo $x . ' => ' . $colors[$x][0] . '<br>';
}
?>
RESULT
array(3) {
[0]=>
array(1) {
[0]=>
string(4) "BLUE"
}
[1]=>
array(1) {
[0]=>
string(5) "GREEN"
}
[2]=>
array(1) {
[0]=>
string(3) "RED"
}
}
or
0 => RED
1 => GREEN
2 => BLUE
EXERCISE
<?php
echo "Testing uasort() : basic functionality.";
// comparison function
function cmp($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else
return -1;
}
// Int array with default keys
$int_values = array(1, 8, 9, 3, 2, 6, 7);
echo "<br><br>Numeric array with default keys:<br><br>";
var_dump( uasort($int_values, 'cmp') );
echo '<br><pre>';
var_dump($int_values);
echo '</pre>';
// String array with default keys
$string_values = array("This", "is", 'a', "test");
echo "<br><br>String array with default keys:<br><br>";
var_dump( uasort($string_values, 'cmp') );
echo '<br><pre>';
var_dump($string_values);
echo '</pre>';
// Associative array with numeric keys
$numeric_key_arg = array(1=> 1, 2 => 2, 3 => 7, 5 => 4, 4 => 9);
echo "<br><br>Associative array with numeric keys:<br><br>";
var_dump( uasort($numeric_key_arg, 'cmp') );
echo '<br><pre>';
var_dump($numeric_key_arg);
echo '</pre>';
// Associative array with string keys
$string_key_arg = array('one' => 4, 'two' => 2, 'three' => 1, 'four' => 10);
echo "<br><br>Associative array with string keys:<br><br>";
var_dump( uasort($string_key_arg, 'cmp') );
echo '<br><pre>';
var_dump($string_key_arg);
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing uasort() : basic functionality
with duplicate values.";
// comparison function
function cmp($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else
return -1;
}
// increasing values
$int_values1 = array(1, 1, 2, 2, 3, 3);
echo "<br><br>Numeric array with
increasing values:<br><br>";
var_dump( uasort($int_values1, 'cmp') );
echo '<br><pre>';
var_dump($int_values1);
echo '</pre>';
// decreasing values
$int_values2 = array(3, 3, 2, 2, 1, 1);
echo "<br><br>Numeric array with
decreasing values:<br><br>";
var_dump( uasort($int_values2, 'cmp') );
echo '<br><pre>';
var_dump($int_values2);
echo '</pre>';
// increasing and decreasing values
$int_values3 = array(1, 2, 3, 3, 2, 1);
echo "<br><br>Numeric array with
increasing and decreasing values:<br><br>";
var_dump( uasort($int_values3, 'cmp') );
echo '<br><pre>';
var_dump($int_values3);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Testing uasort() with 'array_arg'
* containing different reference variables
*/
// comparison function
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else {
return -1;
}
}
echo "Testing uasort() : 'array_arg'
with elements as reference.";
// different variables which are used
// as elements of 'array_arg'
$value1 = -5;
$value2 = 100;
$value3 = 0;
$value4 = &$value1;
// array_args an array containing
// elements with reference variables
$array_arg = array(
0 => 10,
1 => &$value4,
2 => &$value2,
3 => 200,
4 => &$value3,
);
echo "<br><br>Sorting 'array_arg' containing
different references:<br>";
echo '<br>';
var_dump( uasort($array_arg, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
?>
EXERCISE
<?php
/* Testing uasort() with different
* associative arrays having keys as
* string, integer, default & duplicate keys
*/
echo "Testing uasort() : sorting different associative arrays.";
// comparison function
function cmp($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else
return -1;
}
// Array with duplicate string and integer keys
$array_arg = array(0 => 2, "a" => 8,
"d" => 9, 3 => 3,
5 => 2, "o" => 6,
"z" => -99, 0 => 1,
"z" => 3);
echo "<br><br>Array with duplicate keys:<br><br>";
var_dump( uasort($array_arg, 'cmp') );
echo '<pre>';
var_dump($array_arg);
echo '</pre>';
// Array with default and assigned keys
$array_arg = array(0 => "Banana",
1 => "Mango", "Orange",
2 => "Apple", "Pineapple");
echo "<br><br>Array with default/assigned keys:<br><br>";
var_dump( uasort($array_arg, 'cmp') );
echo '<pre>';
var_dump($array_arg);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Testing uasort() with 'array_arg'
* having different keys
*/
echo "Testing uasort() : Sorting array with all possible keys.";
//comparison function
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return -1;
}
else {
return 1;
}
}
// different heredoc strings
//empty heredoc string
$empty_heredoc = <<<EOT1
EOT1;
// single line heredoc string
$simple_heredoc = <<<EOT2
simple
EOT2;
// multiline heredoc string
$multiline_heredoc = <<<EOT3
multiline heredoc with 123
and speci@! ch@r..\ncheck\talso
EOT3;
$array_arg = array(
// default key
1,
//expecting: default key 0,
// value will be replaced by 'FALSE'
// numeric keys
1 => 10,
// expecting: value will be replaced by 'TRUE'
-2 => 9,
8.9 => 8,
012 => 7,
0x34 => 6,
// string keys
'key' => 5,
//single quoted key
"two" => 4,
//double quoted key
'' => 3,
"" => 2,
" " => 0,
// space as key
// bool keys
true => 15,
false => 5,
TRUE => 100,
FALSE => 25,
// null keys
null => 20,
// expecting: value will be replaced by 'NULL'
NULL => 35,
// binary key
"a".chr(0)."b" => 45,
b"binary" => 30,
//heredoc keys
$empty_heredoc => 90,
$simple_heredoc => 75,
$multiline_heredoc => 200,
);
echo "<br><br>Sorted array after uasort() function call:<br><br>";
var_dump( uasort($array_arg, 'cmp_function') );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
?>
EXERCISE
<?php
/*
* sorting different types of
* numeric arrays containing data
* of following type:
* integer, octal, hexadecimal & float
*/
// comparison function
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else {
return -1;
}
}
echo "Testing uasort() : different numeric
arrays as 'array_arg'.";
// Int array
$int_values = array(0 => 3, 1 => 2,
3 => 100, 4 => 150,
5 => 25, 6 => 350,
7 => 0, 8 => -3,
9 => -1200);
echo "<br><br>Sorting Integer array:<br><br>";
var_dump( uasort($int_values, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($int_values);
echo '</pre>';
// Octal array
$octal_values = array(0 => 056, 1 => 023,
2 => 00, 3 => 015,
4 => -045, 5 => 01,
6 => -07);
echo "<br><br>Sorting Octal array:<br><br>";
var_dump( uasort($octal_values, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($octal_values);
echo '</pre>';
// Hexadecimal array
$hex_values = array(0 => 0xAE, 1 => 0x2B,
2 => 0X10, 3 => -0xCF,
4 => 0X12, 5 => -0XF2);
echo "<br><br>Sorting Hex array:<br><br>";
var_dump( uasort($hex_values, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($hex_values);
echo '</pre>';
// Float array
$float_values = array( 0 => 10.2, 1 => 2.4,
2 => -3.4, 3 => 0, 4 => 0.5,
5 => 7.3e3, 6 => -9.34E-2);
echo "<br><br>Sorting Float array:<br><br>";
var_dump( uasort($float_values, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($float_values);
echo '</pre>';
// empty array
$empty_array = array();
echo "<br><br>Sorting empty array:<br><br>";
var_dump( uasort($empty_array, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($empty_array);
echo '</pre>';
?>
EXERCISE
<?php
/*
* sorting different strings:
* single quoted, double quoted and heredoc strings
*/
// comparison function
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else {
return -1;
}
}
// Different heredoc strings to be sorted
$empty_heredoc =<<<EOT
EOT;
$simple_heredoc1 =<<<EOT
Heredoc
EOT;
$simple_heredoc2 =<<<EOT
HEREDOC
EOT;
$multiline_heredoc =<<<EOT
heredoc string\twith!@# and 123
Test this!!!
EOT;
echo "Testing uasort() : different string arrays
as 'array_arg'.";
// Single quoted strings
$single_quoted_values = [
0 => ' ', 1 => 'test',
3 => 'Hello', 4 => 'HELLO',
5 => '', 6 => '\t',
7 => '0', 8 => '123Hello',
9 => '\'', 10 => '@#$%'
];
echo "<br><br>Sorting Single Quoted String values:<br><br>";
var_dump( uasort($single_quoted_values, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($single_quoted_values);
echo '</pre>';
// Double quoted strings
$double_quoted_values = [
0 => " ", 1 => "test",
3 => "Hello", 4 => "HELLO",
5 => "", 6 => "\t",
7 => "0", 8 => "123Hello",
9 => "\"", 10 => "@#$%"
];
echo "<br><br>Sorting Double Quoted String values:<br><br>";
var_dump( uasort($double_quoted_values, 'cmp_function') ); // expecting: bool(true)
echo '<br><pre>';
var_dump($double_quoted_values);
echo '</pre>';
// Heredoc strings
$heredoc_values = array(0 => $empty_heredoc, 1 => $simple_heredoc1, 2 => $simple_heredoc2, 3 => $multiline_heredoc);
echo "<br><br>Sorting Heredoc String values:<br><br>";
var_dump( uasort($heredoc_values, 'cmp_function') ); // expecting: bool(true)
echo '<br><pre>';
var_dump($heredoc_values);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Testing uasort() with 'array_arg'
* having different subarrays as array elements
*/
// comparison function
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else {
return -1;
}
}
echo "Testing uasort() : sorting array having
different subarrays.<br><br>";
$array_args = array(
0 => array(2, 10, -1),
1 => array(100),
2 => array(),
3 => array(0),
4 => array(-1),
5 => array(-9, 34, 54, 0, 20),
6 => array(''),
7 => array("apple", "Apple", "APPLE", "aPPle", "aPpLe")
);
$temp_array = $array_args;
// sorting array_arg as whole array
var_dump( uasort($temp_array, 'cmp_function') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($temp_array);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Passing different anonymous
* functions as 'cmp_function'
* arguments passed by value
* arguments passed by reference
*/
echo "Testing uasort() : anonymous
function as 'cmp_function'.";
$cmp_function = function($value1, $value2) {
if ($value1 == $value2) { return 0; }
else if ($value1 > $value2) { return 1; }
else { return -1; }
};
$array_arg = array(0 => 100,
1 => 3,
2 => -70,
3 => 24,
4 => 90);
echo "<br><br>Anonymous 'cmp_function'
with parameters passed by value:<br><br>";
var_dump( uasort($array_arg, $cmp_function) );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
$cmp_function = function(&$value1, &$value2) {
if ($value1 == $value2) { return 0; }
else if ($value1 > $value2) { return 1; }
else { return -1; }
};
$array_arg = array("b" => "Banana",
"m" => "Mango",
"a" => "Apple",
"p" => "Pineapple");
echo "<br><br>Anonymous 'cmp_function'
with parameters passed by reference:<br><br>";
var_dump( uasort($array_arg, $cmp_function ) );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Passing different built-in
* library functions in place
* of 'cmp_function'
* valid comparison functions:
* strcmp() & strcasecmp()
*/
echo "Testing uasort() : built in function
as 'cmp_function'.";
// Initializing variables
$array_arg = [ "b" => "Banana",
"m" => "Mango",
"a" => "apple",
"p" => "Pineapple",
"o" => "orange" ];
$builtin_fun_arg = $array_arg;
$languageConstruct_fun_arg = $array_arg;
// Testing library functions as comparison function
echo "<br><br>Testing uasort()
with built-in 'cmp_function': strcasecmp():<br><br>";
var_dump( uasort($builtin_fun_arg, 'strcasecmp') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($builtin_fun_arg);
echo '</pre>';
echo "<br><br>Testing uasort()
with built-in 'cmp_function': strcmp():<br><br>";
var_dump( uasort($array_arg, 'strcmp') );
// expecting: bool(true)
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
?>
EXERCISE
<?php
/* Sort the entries by values
* user defined function.
* Source code: ext/spl/spl_array.c
* Alias to functions:
*/
echo "Testing ArrayObject::uasort() : 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(array(2,3,1));
$ao->uasort('cmp');
echo '<br><pre>';
var_dump($ao);
echo '</pre>';
?>
EXERCISE
<?php
/* Sort the entries by values
* user defined function.
* Source code: ext/spl/spl_array.c
* Alias to functions:
*/
$ao = new ArrayObject();
try {
$ao->uasort();
} catch (ArgumentCountError $e) {
echo $e->getMessage() . "<br>";
}
try {
$ao->uasort(1,2);
} catch (ArgumentCountError $e) {
echo $e->getMessage() . "<br>";
}
?>
EXERCISE
<?php
/*
* This testcase tests uasort() functionality
* with different objects
* Objects of different classes:
* simple class,
* child class,
* empty class &
* static class
*/
echo "Testing uasort() : object functionality.";
// comparison function
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else
return -1;
}
// Simple class with single member variable
class SimpleClass
{
private $int_value;
public function __construct($value) {
$this->int_value = $value;
}
}
// Class without any member
class EmptyClass
{
}
// Class with static member
class StaticClass
{
public static $static_value;
public function __construct($value) {
StaticClass::$static_value = $value;
}
}
// Abstract class
abstract class AbstractClass
{
public $pub_value;
public abstract function abstractMethod();
}
// Child class extending abstract class
class ChildClass extends AbstractClass
{
public $child_value = 100;
public function abstractMethod() {
$pub_value = 5;
}
public function __construct($value) {
$this->child_value = $value;
}
}
// Testing uasort with StaticClass
// objects as elements of 'array_arg'
echo "<br><br>Testing uasort()
with StaticClass objects:<br><br>";
$array_arg = array(
0 => new StaticClass(20),
1 => new StaticClass(50),
2 => new StaticClass(15),
3 => new StaticClass(70),
);
var_dump( uasort($array_arg, 'cmp_function') );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
// Testing uasort with EmptyClass
// objects as elements of 'array_arg'
echo "<br><br>Testing uasort() with
EmptyClass objects:<br><br>";
$array_arg = array(
0 => new EmptyClass(),
1 => new EmptyClass(),
2 => new EmptyClass(),
3 => new EmptyClass(),
);
var_dump( uasort($array_arg, 'cmp_function') );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
// Testing uasort with ChildClass
// objects as elements of 'array_arg'
echo "<br><br>Testing uasort()
with ChildClass objects:<br><br>";
$array_arg = array(
0 => new ChildClass(20),
1 => new ChildClass(500),
2 => new ChildClass(15),
3 => new ChildClass(700),
);
var_dump( uasort($array_arg, 'cmp_function') );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Testing uasort() function with
* the array of objects
* array of objects which has only one
* member variable & more than
* one member variables
*/
echo "Testing uasort() : object functionality.<br><br>";
// comparison function
function simple_cmp($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else
return -1;
}
// comparison function for SimpleClass2
// objects which has more than one members
function multiple_cmp($value1, $value2)
{
if($value1->getValue() == $value2->getValue())
return 0;
else if($value1->getValue() > $value2->getValue())
return 1;
else
return -1;
}
// Simple class with single member variable
class SimpleClass1
{
private $int_value;
public function __construct($value) {
$this->int_value = $value;
}
}
// Simple class with more than one member variables
class SimpleClass2
{
private $int_value;
protected $float_value;
public $string_value;
public function __construct($int, $float, $str) {
$this->int_value = $int;
$this->float_value = $float;
$this->string_value = $str;
}
public function getValue() {
return $this->int_value;
}
}
// array of SimpleClass objects with only one member
$array_arg = array(
0 => new SimpleClass1(10),
1 => new SimpleClass1(1),
2 => new SimpleClass1(100),
3 => new SimpleClass1(50)
);
var_dump( uasort($array_arg, 'simple_cmp') );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
// array of SimpleClass objects having more than one members
$array_arg = array(
0 => new SimpleClass2(2, 3.4, "mango"),
1 => new SimpleClass2(10, 1.2, "apple"),
2 => new SimpleClass2(5, 2.5, "orange"),
);
var_dump( uasort($array_arg, 'multiple_cmp') );
echo '<br><pre>';
var_dump($array_arg);
echo '</pre>';
?>