1--TEST-- 2Test uasort() function : usage variations - different associative arrays 3--FILE-- 4<?php 5/* Prototype : bool uasort(array $array_arg, string $cmp_function) 6 * Description: Sort an array with a user-defined comparison function and maintain index association 7 * Source code: ext/standard/array.c 8*/ 9 10/* Testing uasort() with different associative arrays having keys as 11 * string, integer, default & duplicate keys 12 */ 13 14echo "*** Testing uasort() : sorting different associative arrays ***\n"; 15 16// comparison function 17/* Prototype : int cmp(mixed $value1, mixed $value2) 18 * Parameters : $value1 and $value2 - values to be compared 19 * Return value : 0 - if both values are same 20 * 1 - if value1 is greater than value2 21 * -1 - if value1 is less than value2 22 * Description : compares value1 and value2 23 */ 24function cmp($value1, $value2) 25{ 26 if($value1 == $value2) { 27 return 0; 28 } 29 else if($value1 > $value2) { 30 return 1; 31 } 32 else 33 return -1; 34} 35 36// Array with duplicate string and integer keys 37$array_arg = array(0 => 2, "a" => 8, "d" => 9, 3 => 3, 5 => 2, "o" => 6, "z" => -99, 0 => 1, "z" => 3); 38echo "-- Array with duplicate keys --\n"; 39var_dump( uasort($array_arg, 'cmp') ); 40var_dump($array_arg); 41 42// Array with default and assigned keys 43$array_arg = array(0 => "Banana", 1 => "Mango", "Orange", 2 => "Apple", "Pineapple"); 44echo "-- Array with default/assigned keys --\n"; 45var_dump( uasort($array_arg, 'cmp') ); 46var_dump($array_arg); 47 48echo "Done" 49?> 50--EXPECTF-- 51*** Testing uasort() : sorting different associative arrays *** 52-- Array with duplicate keys -- 53bool(true) 54array(7) { 55 [0]=> 56 int(1) 57 [5]=> 58 int(2) 59 [3]=> 60 int(3) 61 ["z"]=> 62 int(3) 63 ["o"]=> 64 int(6) 65 ["a"]=> 66 int(8) 67 ["d"]=> 68 int(9) 69} 70-- Array with default/assigned keys -- 71bool(true) 72array(4) { 73 [2]=> 74 string(5) "Apple" 75 [0]=> 76 string(6) "Banana" 77 [1]=> 78 string(5) "Mango" 79 [3]=> 80 string(9) "Pineapple" 81} 82Done 83