1--TEST-- 2Test arsort() function : object functionality - sort objects 3--FILE-- 4<?php 5/* Prototype : bool arsort ( array &$array [, int $asort_flags] ) 6 * Description: Sort an array and maintain index association. 7 Elements will be arranged from highest to lowest when this function has completed. 8 * Source code: ext/standard/array.c 9*/ 10 11/* 12 * testing arsort() by providing integer/string object arrays with following flag values 13 * 1. Defualt flag value 14 * 2. SORT_REGULAR - compare items normally 15*/ 16 17echo "*** Testing arsort() : object functionality ***\n"; 18 19// class declaration for integer objects 20class for_integer_arsort 21{ 22 public $class_value; 23 // initializing object member value 24 function __construct($value){ 25 $this->class_value = $value; 26 } 27 28} 29 30// class declaration for string objects 31class for_string_arsort 32{ 33 public $class_value; 34 // initializing object member value 35 function __construct($value){ 36 $this->class_value = $value; 37 } 38 39 // return string value 40 function __tostring() { 41 return (string)$this->value; 42 } 43 44} 45 46// array of integer objects 47$unsorted_int_obj = array ( 48 1 => new for_integer_arsort(11), 2 => new for_integer_arsort(66), 49 3 => new for_integer_arsort(23), 4 => new for_integer_arsort(-5), 50 5 => new for_integer_arsort(0.001), 6 => new for_integer_arsort(0) 51); 52 53// array of string objects 54$unsorted_str_obj = array ( 55 "a" => new for_string_arsort("axx"), "b" => new for_string_arsort("t"), 56 "c" => new for_string_arsort("w"), "d" => new for_string_arsort("py"), 57 "e" => new for_string_arsort("apple"), "f" => new for_string_arsort("Orange"), 58 "g" => new for_string_arsort("Lemon"), "h" => new for_string_arsort("aPPle") 59); 60 61 62echo "\n-- Testing arsort() by supplying various object arrays, 'flag' value is defualt --\n"; 63 64// testing arsort() function by supplying integer object array, flag value is defualt 65$temp_array = $unsorted_int_obj; 66var_dump(arsort($temp_array) ); 67var_dump($temp_array); 68 69// testing arsort() function by supplying string object array, flag value is defualt 70$temp_array = $unsorted_str_obj; 71var_dump(arsort($temp_array) ); 72var_dump($temp_array); 73 74echo "\n-- Testing arsort() by supplying various object arrays, 'flag' value is SORT_REGULAR --\n"; 75// testing arsort() function by supplying integer object array, flag value = SORT_REGULAR 76$temp_array = $unsorted_int_obj; 77var_dump(arsort($temp_array, SORT_REGULAR) ); 78var_dump($temp_array); 79 80// testing arsort() function by supplying string object array, flag value = SORT_REGULAR 81$temp_array = $unsorted_str_obj; 82var_dump(arsort($temp_array, SORT_REGULAR) ); 83var_dump($temp_array); 84 85echo "Done\n"; 86?> 87--EXPECT-- 88*** Testing arsort() : object functionality *** 89 90-- Testing arsort() by supplying various object arrays, 'flag' value is defualt -- 91bool(true) 92array(6) { 93 [2]=> 94 object(for_integer_arsort)#2 (1) { 95 ["class_value"]=> 96 int(66) 97 } 98 [3]=> 99 object(for_integer_arsort)#3 (1) { 100 ["class_value"]=> 101 int(23) 102 } 103 [1]=> 104 object(for_integer_arsort)#1 (1) { 105 ["class_value"]=> 106 int(11) 107 } 108 [5]=> 109 object(for_integer_arsort)#5 (1) { 110 ["class_value"]=> 111 float(0.001) 112 } 113 [6]=> 114 object(for_integer_arsort)#6 (1) { 115 ["class_value"]=> 116 int(0) 117 } 118 [4]=> 119 object(for_integer_arsort)#4 (1) { 120 ["class_value"]=> 121 int(-5) 122 } 123} 124bool(true) 125array(8) { 126 ["c"]=> 127 object(for_string_arsort)#9 (1) { 128 ["class_value"]=> 129 string(1) "w" 130 } 131 ["b"]=> 132 object(for_string_arsort)#8 (1) { 133 ["class_value"]=> 134 string(1) "t" 135 } 136 ["d"]=> 137 object(for_string_arsort)#10 (1) { 138 ["class_value"]=> 139 string(2) "py" 140 } 141 ["a"]=> 142 object(for_string_arsort)#7 (1) { 143 ["class_value"]=> 144 string(3) "axx" 145 } 146 ["e"]=> 147 object(for_string_arsort)#11 (1) { 148 ["class_value"]=> 149 string(5) "apple" 150 } 151 ["h"]=> 152 object(for_string_arsort)#14 (1) { 153 ["class_value"]=> 154 string(5) "aPPle" 155 } 156 ["f"]=> 157 object(for_string_arsort)#12 (1) { 158 ["class_value"]=> 159 string(6) "Orange" 160 } 161 ["g"]=> 162 object(for_string_arsort)#13 (1) { 163 ["class_value"]=> 164 string(5) "Lemon" 165 } 166} 167 168-- Testing arsort() by supplying various object arrays, 'flag' value is SORT_REGULAR -- 169bool(true) 170array(6) { 171 [2]=> 172 object(for_integer_arsort)#2 (1) { 173 ["class_value"]=> 174 int(66) 175 } 176 [3]=> 177 object(for_integer_arsort)#3 (1) { 178 ["class_value"]=> 179 int(23) 180 } 181 [1]=> 182 object(for_integer_arsort)#1 (1) { 183 ["class_value"]=> 184 int(11) 185 } 186 [5]=> 187 object(for_integer_arsort)#5 (1) { 188 ["class_value"]=> 189 float(0.001) 190 } 191 [6]=> 192 object(for_integer_arsort)#6 (1) { 193 ["class_value"]=> 194 int(0) 195 } 196 [4]=> 197 object(for_integer_arsort)#4 (1) { 198 ["class_value"]=> 199 int(-5) 200 } 201} 202bool(true) 203array(8) { 204 ["c"]=> 205 object(for_string_arsort)#9 (1) { 206 ["class_value"]=> 207 string(1) "w" 208 } 209 ["b"]=> 210 object(for_string_arsort)#8 (1) { 211 ["class_value"]=> 212 string(1) "t" 213 } 214 ["d"]=> 215 object(for_string_arsort)#10 (1) { 216 ["class_value"]=> 217 string(2) "py" 218 } 219 ["a"]=> 220 object(for_string_arsort)#7 (1) { 221 ["class_value"]=> 222 string(3) "axx" 223 } 224 ["e"]=> 225 object(for_string_arsort)#11 (1) { 226 ["class_value"]=> 227 string(5) "apple" 228 } 229 ["h"]=> 230 object(for_string_arsort)#14 (1) { 231 ["class_value"]=> 232 string(5) "aPPle" 233 } 234 ["f"]=> 235 object(for_string_arsort)#12 (1) { 236 ["class_value"]=> 237 string(6) "Orange" 238 } 239 ["g"]=> 240 object(for_string_arsort)#13 (1) { 241 ["class_value"]=> 242 string(5) "Lemon" 243 } 244} 245Done 246