1--TEST-- 2Test uasort() function : basic functionality 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 10echo "*** Testing uasort() : basic functionality ***\n"; 11 12// comparison function 13/* Prototype : int cmp(mixed $value1, mixed $value2) 14 * Parameters : $value1 and $value2 - values to be compared 15 * Return value : 0 - if both values are same 16 * 1 - if value1 is greater than value2 17 * -1 - if value1 is less than value2 18 * Description : compares value1 and value2 19 */ 20function cmp($value1, $value2) 21{ 22 if($value1 == $value2) { 23 return 0; 24 } 25 else if($value1 > $value2) { 26 return 1; 27 } 28 else 29 return -1; 30} 31 32// Int array with default keys 33$int_values = array(1, 8, 9, 3, 2, 6, 7); 34echo "-- Numeric array with default keys --\n"; 35var_dump( uasort($int_values, 'cmp') ); 36var_dump($int_values); 37 38// String array with default keys 39$string_values = array("This", "is", 'a', "test"); 40echo "-- String array with default keys --\n"; 41var_dump( uasort($string_values, 'cmp') ); 42var_dump($string_values); 43 44// Associative array with numeric keys 45$numeric_key_arg = array(1=> 1, 2 => 2, 3 => 7, 5 => 4, 4 => 9); 46echo "-- Associative array with numeric keys --\n"; 47var_dump( uasort($numeric_key_arg, 'cmp') ); 48var_dump($numeric_key_arg); 49 50// Associative array with string keys 51$string_key_arg = array('one' => 4, 'two' => 2, 'three' => 1, 'four' => 10); 52echo "-- Associative array with string keys --\n"; 53var_dump( uasort($string_key_arg, 'cmp') ); 54var_dump($string_key_arg); 55 56echo "Done" 57?> 58--EXPECT-- 59*** Testing uasort() : basic functionality *** 60-- Numeric array with default keys -- 61bool(true) 62array(7) { 63 [0]=> 64 int(1) 65 [4]=> 66 int(2) 67 [3]=> 68 int(3) 69 [5]=> 70 int(6) 71 [6]=> 72 int(7) 73 [1]=> 74 int(8) 75 [2]=> 76 int(9) 77} 78-- String array with default keys -- 79bool(true) 80array(4) { 81 [0]=> 82 string(4) "This" 83 [2]=> 84 string(1) "a" 85 [1]=> 86 string(2) "is" 87 [3]=> 88 string(4) "test" 89} 90-- Associative array with numeric keys -- 91bool(true) 92array(5) { 93 [1]=> 94 int(1) 95 [2]=> 96 int(2) 97 [5]=> 98 int(4) 99 [3]=> 100 int(7) 101 [4]=> 102 int(9) 103} 104-- Associative array with string keys -- 105bool(true) 106array(4) { 107 ["three"]=> 108 int(1) 109 ["two"]=> 110 int(2) 111 ["one"]=> 112 int(4) 113 ["four"]=> 114 int(10) 115} 116Done 117