1--TEST-- 2Test sort() function : usage variations - sort integer/float values 3--FILE-- 4<?php 5/* Prototype : bool sort ( array &$array [, int $sort_flags] ) 6 * Description: This function sorts an array. 7 Elements will be arranged from lowest to highest when this function has completed. 8 * Source code: ext/standard/array.c 9*/ 10 11/* 12 * Testing sort() by providing different integer/float value arrays for $array argument 13 * with following flag values 14 * 1. flag value as defualt 15 * 2. SORT_REGULAR - compare items normally 16 * 3. SORT_NUMERIC - compare items numerically 17 * 4. SORT_STRING - compare items as strings 18*/ 19 20echo "*** Testing sort() : usage variations ***\n"; 21 22// group of various arrays 23$various_arrays = array ( 24 // negative/posative integers array 25 array(11, -11, 21, -21, 31, -31, 0, 41, -41), 26 27 // float value array 28 array(10.5, -10.5, 10.5e2, 10.6E-2, .5, .01, -.1), 29 30 // mixed value array 31 array(.0001, .0021, -.01, -1, 0, .09, 2, -.9, 10.6E-2, -10.6E-2, 33), 32 33 // array values contains minimum and maximum ranges 34 array(2147483647, 2147483648, -2147483647, -2147483648, -0, 0, -2147483649) 35); 36 37// set of possible flag values 38$flag_value = array("SORT_REGULAR" => SORT_REGULAR, "SORT_NUMERIC" => SORT_NUMERIC); 39 40$count = 1; 41echo "\n-- Testing sort() by supplying various integer/float arrays --\n"; 42 43// loop through to test sort() with different arrays 44foreach ($various_arrays as $array) { 45 echo "\n-- Iteration $count --\n"; 46 47 echo "- With Defualt sort flag -\n"; 48 $temp_array = $array; 49 var_dump(sort($temp_array) ); 50 var_dump($temp_array); 51 52 // loop through $flag_value array and setting all possible flag values 53 foreach($flag_value as $key => $flag){ 54 echo "- Sort flag = $key -\n"; 55 $temp_array = $array; 56 var_dump(sort($temp_array, $flag) ); 57 var_dump($temp_array); 58 } 59 $count++; 60} 61 62echo "Done\n"; 63?> 64--EXPECTF-- 65*** Testing sort() : usage variations *** 66 67-- Testing sort() by supplying various integer/float arrays -- 68 69-- Iteration 1 -- 70- With Defualt sort flag - 71bool(true) 72array(9) { 73 [0]=> 74 int(-41) 75 [1]=> 76 int(-31) 77 [2]=> 78 int(-21) 79 [3]=> 80 int(-11) 81 [4]=> 82 int(0) 83 [5]=> 84 int(11) 85 [6]=> 86 int(21) 87 [7]=> 88 int(31) 89 [8]=> 90 int(41) 91} 92- Sort flag = SORT_REGULAR - 93bool(true) 94array(9) { 95 [0]=> 96 int(-41) 97 [1]=> 98 int(-31) 99 [2]=> 100 int(-21) 101 [3]=> 102 int(-11) 103 [4]=> 104 int(0) 105 [5]=> 106 int(11) 107 [6]=> 108 int(21) 109 [7]=> 110 int(31) 111 [8]=> 112 int(41) 113} 114- Sort flag = SORT_NUMERIC - 115bool(true) 116array(9) { 117 [0]=> 118 int(-41) 119 [1]=> 120 int(-31) 121 [2]=> 122 int(-21) 123 [3]=> 124 int(-11) 125 [4]=> 126 int(0) 127 [5]=> 128 int(11) 129 [6]=> 130 int(21) 131 [7]=> 132 int(31) 133 [8]=> 134 int(41) 135} 136 137-- Iteration 2 -- 138- With Defualt sort flag - 139bool(true) 140array(7) { 141 [0]=> 142 float(-10.5) 143 [1]=> 144 float(-0.1) 145 [2]=> 146 float(0.01) 147 [3]=> 148 float(0.106) 149 [4]=> 150 float(0.5) 151 [5]=> 152 float(10.5) 153 [6]=> 154 float(1050) 155} 156- Sort flag = SORT_REGULAR - 157bool(true) 158array(7) { 159 [0]=> 160 float(-10.5) 161 [1]=> 162 float(-0.1) 163 [2]=> 164 float(0.01) 165 [3]=> 166 float(0.106) 167 [4]=> 168 float(0.5) 169 [5]=> 170 float(10.5) 171 [6]=> 172 float(1050) 173} 174- Sort flag = SORT_NUMERIC - 175bool(true) 176array(7) { 177 [0]=> 178 float(-10.5) 179 [1]=> 180 float(-0.1) 181 [2]=> 182 float(0.01) 183 [3]=> 184 float(0.106) 185 [4]=> 186 float(0.5) 187 [5]=> 188 float(10.5) 189 [6]=> 190 float(1050) 191} 192 193-- Iteration 3 -- 194- With Defualt sort flag - 195bool(true) 196array(11) { 197 [0]=> 198 int(-1) 199 [1]=> 200 float(-0.9) 201 [2]=> 202 float(-0.106) 203 [3]=> 204 float(-0.01) 205 [4]=> 206 int(0) 207 [5]=> 208 float(0.0001) 209 [6]=> 210 float(0.0021) 211 [7]=> 212 float(0.09) 213 [8]=> 214 float(0.106) 215 [9]=> 216 int(2) 217 [10]=> 218 int(33) 219} 220- Sort flag = SORT_REGULAR - 221bool(true) 222array(11) { 223 [0]=> 224 int(-1) 225 [1]=> 226 float(-0.9) 227 [2]=> 228 float(-0.106) 229 [3]=> 230 float(-0.01) 231 [4]=> 232 int(0) 233 [5]=> 234 float(0.0001) 235 [6]=> 236 float(0.0021) 237 [7]=> 238 float(0.09) 239 [8]=> 240 float(0.106) 241 [9]=> 242 int(2) 243 [10]=> 244 int(33) 245} 246- Sort flag = SORT_NUMERIC - 247bool(true) 248array(11) { 249 [0]=> 250 int(-1) 251 [1]=> 252 float(-0.9) 253 [2]=> 254 float(-0.106) 255 [3]=> 256 float(-0.01) 257 [4]=> 258 int(0) 259 [5]=> 260 float(0.0001) 261 [6]=> 262 float(0.0021) 263 [7]=> 264 float(0.09) 265 [8]=> 266 float(0.106) 267 [9]=> 268 int(2) 269 [10]=> 270 int(33) 271} 272 273-- Iteration 4 -- 274- With Defualt sort flag - 275bool(true) 276array(7) { 277 [0]=> 278 %s(-2147483649) 279 [1]=> 280 %s(-2147483648) 281 [2]=> 282 int(-2147483647) 283 [3]=> 284 int(0) 285 [4]=> 286 int(0) 287 [5]=> 288 int(2147483647) 289 [6]=> 290 %s(2147483648) 291} 292- Sort flag = SORT_REGULAR - 293bool(true) 294array(7) { 295 [0]=> 296 %s(-2147483649) 297 [1]=> 298 %s(-2147483648) 299 [2]=> 300 int(-2147483647) 301 [3]=> 302 int(0) 303 [4]=> 304 int(0) 305 [5]=> 306 int(2147483647) 307 [6]=> 308 %s(2147483648) 309} 310- Sort flag = SORT_NUMERIC - 311bool(true) 312array(7) { 313 [0]=> 314 %s(-2147483649) 315 [1]=> 316 %s(-2147483648) 317 [2]=> 318 int(-2147483647) 319 [3]=> 320 int(0) 321 [4]=> 322 int(0) 323 [5]=> 324 int(2147483647) 325 [6]=> 326 %s(2147483648) 327} 328Done 329