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