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