1--TEST-- 2Test krsort() function : usage variations - sort integer/float values 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/* 11 * Testing krsort() by providing array of integer/float/mixed values for $array argument 12 * with following flag values: 13 * 1.flag value as defualt 14 * 2.SORT_REGULAR - compare items normally 15 * 3.SORT_NUMERIC - compare items numerically 16*/ 17 18echo "*** Testing krsort() : usage variations ***\n"; 19 20// diff. associative arrays to sort 21$various_arrays = array( 22 // negative/posative integer key value array 23 array(1 => 11, -2 => -11, 3 => 21, -4 => -21, 5 => 31, -6 => -31, 7 => 0, 8 => 41, -10 =>-41), 24 25 // float key values 26 array(1.0 => 10.5, 0.2 => -10.5, 3.1 => 10.5e2, 4 => 10.6E-2, .5 => .5, 6 => .0001, -7 => -.1), 27 28 // mixed value array with different types of keys 29 array(1 => .0001, 2 => .0021, -3 => -.01, 4 => -1, 5 => 0, 6 => .09, 7 => 2, -8 => -.9, 9 => 10.6E-2, 30 -10 => -10.6E-2, 11 => 33) 31); 32 33// set of possible flag values 34$flags = array("SORT_REGULAR" => SORT_REGULAR, "SORT_NUMERIC" => SORT_NUMERIC); 35 36$count = 1; 37echo "\n-- Testing krsort() by supplying various integer/float arrays --\n"; 38 39// loop through to test krsort() with different arrays 40foreach ($various_arrays as $array) { 41 echo "\n-- Iteration $count --\n"; 42 43 echo "- With defualt sort flag -\n"; 44 $temp_array = $array; 45 var_dump(krsort($temp_array) ); 46 var_dump($temp_array); 47 48 // loop through $flags array and call krsort() with all possible sort flag values 49 foreach($flags as $key => $flag){ 50 echo "- Sort flag = $key -\n"; 51 $temp_array = $array; 52 var_dump(krsort($temp_array, $flag) ); 53 var_dump($temp_array); 54 } 55 $count++; 56} 57 58echo "Done\n"; 59?> 60--EXPECTF-- 61*** Testing krsort() : usage variations *** 62 63-- Testing krsort() by supplying various integer/float arrays -- 64 65-- Iteration 1 -- 66- With defualt sort flag - 67bool(true) 68array(9) { 69 [8]=> 70 int(41) 71 [7]=> 72 int(0) 73 [5]=> 74 int(31) 75 [3]=> 76 int(21) 77 [1]=> 78 int(11) 79 [-2]=> 80 int(-11) 81 [-4]=> 82 int(-21) 83 [-6]=> 84 int(-31) 85 [-10]=> 86 int(-41) 87} 88- Sort flag = SORT_REGULAR - 89bool(true) 90array(9) { 91 [8]=> 92 int(41) 93 [7]=> 94 int(0) 95 [5]=> 96 int(31) 97 [3]=> 98 int(21) 99 [1]=> 100 int(11) 101 [-2]=> 102 int(-11) 103 [-4]=> 104 int(-21) 105 [-6]=> 106 int(-31) 107 [-10]=> 108 int(-41) 109} 110- Sort flag = SORT_NUMERIC - 111bool(true) 112array(9) { 113 [8]=> 114 int(41) 115 [7]=> 116 int(0) 117 [5]=> 118 int(31) 119 [3]=> 120 int(21) 121 [1]=> 122 int(11) 123 [-2]=> 124 int(-11) 125 [-4]=> 126 int(-21) 127 [-6]=> 128 int(-31) 129 [-10]=> 130 int(-41) 131} 132 133-- Iteration 2 -- 134- With defualt sort flag - 135bool(true) 136array(6) { 137 [6]=> 138 float(0.0001) 139 [4]=> 140 float(0.106) 141 [3]=> 142 float(1050) 143 [1]=> 144 float(10.5) 145 [0]=> 146 float(0.5) 147 [-7]=> 148 float(-0.1) 149} 150- Sort flag = SORT_REGULAR - 151bool(true) 152array(6) { 153 [6]=> 154 float(0.0001) 155 [4]=> 156 float(0.106) 157 [3]=> 158 float(1050) 159 [1]=> 160 float(10.5) 161 [0]=> 162 float(0.5) 163 [-7]=> 164 float(-0.1) 165} 166- Sort flag = SORT_NUMERIC - 167bool(true) 168array(6) { 169 [6]=> 170 float(0.0001) 171 [4]=> 172 float(0.106) 173 [3]=> 174 float(1050) 175 [1]=> 176 float(10.5) 177 [0]=> 178 float(0.5) 179 [-7]=> 180 float(-0.1) 181} 182 183-- Iteration 3 -- 184- With defualt sort flag - 185bool(true) 186array(11) { 187 [11]=> 188 int(33) 189 [9]=> 190 float(0.106) 191 [7]=> 192 int(2) 193 [6]=> 194 float(0.09) 195 [5]=> 196 int(0) 197 [4]=> 198 int(-1) 199 [2]=> 200 float(0.0021) 201 [1]=> 202 float(0.0001) 203 [-3]=> 204 float(-0.01) 205 [-8]=> 206 float(-0.9) 207 [-10]=> 208 float(-0.106) 209} 210- Sort flag = SORT_REGULAR - 211bool(true) 212array(11) { 213 [11]=> 214 int(33) 215 [9]=> 216 float(0.106) 217 [7]=> 218 int(2) 219 [6]=> 220 float(0.09) 221 [5]=> 222 int(0) 223 [4]=> 224 int(-1) 225 [2]=> 226 float(0.0021) 227 [1]=> 228 float(0.0001) 229 [-3]=> 230 float(-0.01) 231 [-8]=> 232 float(-0.9) 233 [-10]=> 234 float(-0.106) 235} 236- Sort flag = SORT_NUMERIC - 237bool(true) 238array(11) { 239 [11]=> 240 int(33) 241 [9]=> 242 float(0.106) 243 [7]=> 244 int(2) 245 [6]=> 246 float(0.09) 247 [5]=> 248 int(0) 249 [4]=> 250 int(-1) 251 [2]=> 252 float(0.0021) 253 [1]=> 254 float(0.0001) 255 [-3]=> 256 float(-0.01) 257 [-8]=> 258 float(-0.9) 259 [-10]=> 260 float(-0.106) 261} 262Done 263