1--TEST-- 2Test ksort() function : usage variations - sort integer/float values 3--FILE-- 4<?php 5/* Prototype : bool ksort ( array &$array [, int $sort_flags] ) 6 * Description: Sort an array by key, maintaining key to data correlation 7 * Source code: ext/standard/array.c 8*/ 9 10/* 11 * Testing ksort() 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 ksort() : 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, 30 9 => 10.6E-2, -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 ksort() by supplying various integer/float arrays --\n"; 38 39// loop through to test ksort() 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(ksort($temp_array) ); 46 var_dump($temp_array); 47 48 // loop through $flags array and call ksort() 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(ksort($temp_array, $flag) ); 53 var_dump($temp_array); 54 } 55 $count++; 56} 57 58echo "Done\n"; 59?> 60--EXPECTF-- 61*** Testing ksort() : usage variations *** 62 63-- Testing ksort() by supplying various integer/float arrays -- 64 65-- Iteration 1 -- 66- With defualt sort flag - 67bool(true) 68array(9) { 69 [-10]=> 70 int(-41) 71 [-6]=> 72 int(-31) 73 [-4]=> 74 int(-21) 75 [-2]=> 76 int(-11) 77 [1]=> 78 int(11) 79 [3]=> 80 int(21) 81 [5]=> 82 int(31) 83 [7]=> 84 int(0) 85 [8]=> 86 int(41) 87} 88- Sort flag = SORT_REGULAR - 89bool(true) 90array(9) { 91 [-10]=> 92 int(-41) 93 [-6]=> 94 int(-31) 95 [-4]=> 96 int(-21) 97 [-2]=> 98 int(-11) 99 [1]=> 100 int(11) 101 [3]=> 102 int(21) 103 [5]=> 104 int(31) 105 [7]=> 106 int(0) 107 [8]=> 108 int(41) 109} 110- Sort flag = SORT_NUMERIC - 111bool(true) 112array(9) { 113 [-10]=> 114 int(-41) 115 [-6]=> 116 int(-31) 117 [-4]=> 118 int(-21) 119 [-2]=> 120 int(-11) 121 [1]=> 122 int(11) 123 [3]=> 124 int(21) 125 [5]=> 126 int(31) 127 [7]=> 128 int(0) 129 [8]=> 130 int(41) 131} 132 133-- Iteration 2 -- 134- With defualt sort flag - 135bool(true) 136array(6) { 137 [-7]=> 138 float(-0.1) 139 [0]=> 140 float(0.5) 141 [1]=> 142 float(10.5) 143 [3]=> 144 float(1050) 145 [4]=> 146 float(0.106) 147 [6]=> 148 float(0.0001) 149} 150- Sort flag = SORT_REGULAR - 151bool(true) 152array(6) { 153 [-7]=> 154 float(-0.1) 155 [0]=> 156 float(0.5) 157 [1]=> 158 float(10.5) 159 [3]=> 160 float(1050) 161 [4]=> 162 float(0.106) 163 [6]=> 164 float(0.0001) 165} 166- Sort flag = SORT_NUMERIC - 167bool(true) 168array(6) { 169 [-7]=> 170 float(-0.1) 171 [0]=> 172 float(0.5) 173 [1]=> 174 float(10.5) 175 [3]=> 176 float(1050) 177 [4]=> 178 float(0.106) 179 [6]=> 180 float(0.0001) 181} 182 183-- Iteration 3 -- 184- With defualt sort flag - 185bool(true) 186array(11) { 187 [-10]=> 188 float(-0.106) 189 [-8]=> 190 float(-0.9) 191 [-3]=> 192 float(-0.01) 193 [1]=> 194 float(0.0001) 195 [2]=> 196 float(0.0021) 197 [4]=> 198 int(-1) 199 [5]=> 200 int(0) 201 [6]=> 202 float(0.09) 203 [7]=> 204 int(2) 205 [9]=> 206 float(0.106) 207 [11]=> 208 int(33) 209} 210- Sort flag = SORT_REGULAR - 211bool(true) 212array(11) { 213 [-10]=> 214 float(-0.106) 215 [-8]=> 216 float(-0.9) 217 [-3]=> 218 float(-0.01) 219 [1]=> 220 float(0.0001) 221 [2]=> 222 float(0.0021) 223 [4]=> 224 int(-1) 225 [5]=> 226 int(0) 227 [6]=> 228 float(0.09) 229 [7]=> 230 int(2) 231 [9]=> 232 float(0.106) 233 [11]=> 234 int(33) 235} 236- Sort flag = SORT_NUMERIC - 237bool(true) 238array(11) { 239 [-10]=> 240 float(-0.106) 241 [-8]=> 242 float(-0.9) 243 [-3]=> 244 float(-0.01) 245 [1]=> 246 float(0.0001) 247 [2]=> 248 float(0.0021) 249 [4]=> 250 int(-1) 251 [5]=> 252 int(0) 253 [6]=> 254 float(0.09) 255 [7]=> 256 int(2) 257 [9]=> 258 float(0.106) 259 [11]=> 260 int(33) 261} 262Done 263