1--TEST-- 2Test ksort() function : usage variations - unexpected values for 'sort_flags' argument 3--FILE-- 4<?php 5/* Prototype : bool ksort(array &array_arg [, 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 different unexpected values for flag argument 12*/ 13 14echo "*** Testing ksort() : usage variations ***\n"; 15 16//get an unset variable 17$unset_var = 10; 18unset ($unset_var); 19 20// resource variable 21$fp = fopen(__FILE__, "r"); 22 23// an array for checking unexpected behavior 24$unsorted_values = array(10 => 10, 2 => 2, 45 => 45); 25 26//array of unexpected values to iterate over 27$unexpected_values = array ( 28 29 // int data 30/*1*/ -2345, 31 32 // float data 33/*2*/ 10.5, 34 -10.5, 35 10.5e2, 36 10.6E-2, 37 .5, 38 39 // null data 40/*7*/ NULL, 41 null, 42 43 // boolean data 44/*9*/ true, 45 false, 46 TRUE, 47 FALSE, 48 49 // empty data 50/*13*/ "", 51 '', 52 53 // string data 54/*15*/ "string", 55 'string', 56 57 // object data 58/*16*/ new stdclass(), 59 60 // undefined data 61/*17*/ @undefined_var, 62 63 // unset data 64/*18*/ @unset_var, 65 66 // resource variable 67/*19*/ $fp 68 69); 70 71// loop though each element of the array and check the working of ksort() 72// when 'sort_flags' argument is supplied with different values 73echo "\n-- Testing ksort() by supplying different unexpected values for 'sort_flags' argument --\n"; 74 75$counter = 1; 76for($index = 0; $index < count($unexpected_values); $index ++) { 77 echo "-- Iteration $counter --\n"; 78 $value = $unexpected_values [$index]; 79 $temp_array = $unsorted_values; 80 var_dump( ksort($temp_array, $value) ); 81 var_dump($temp_array); 82 $counter++; 83} 84 85echo "Done"; 86?> 87--EXPECTF-- 88*** Testing ksort() : usage variations *** 89 90-- Testing ksort() by supplying different unexpected values for 'sort_flags' argument -- 91-- Iteration 1 -- 92bool(true) 93array(3) { 94 [2]=> 95 int(2) 96 [10]=> 97 int(10) 98 [45]=> 99 int(45) 100} 101-- Iteration 2 -- 102bool(true) 103array(3) { 104 [10]=> 105 int(10) 106 [2]=> 107 int(2) 108 [45]=> 109 int(45) 110} 111-- Iteration 3 -- 112bool(true) 113array(3) { 114 [2]=> 115 int(2) 116 [10]=> 117 int(10) 118 [45]=> 119 int(45) 120} 121-- Iteration 4 -- 122bool(true) 123array(3) { 124 [2]=> 125 int(2) 126 [10]=> 127 int(10) 128 [45]=> 129 int(45) 130} 131-- Iteration 5 -- 132bool(true) 133array(3) { 134 [2]=> 135 int(2) 136 [10]=> 137 int(10) 138 [45]=> 139 int(45) 140} 141-- Iteration 6 -- 142bool(true) 143array(3) { 144 [2]=> 145 int(2) 146 [10]=> 147 int(10) 148 [45]=> 149 int(45) 150} 151-- Iteration 7 -- 152bool(true) 153array(3) { 154 [2]=> 155 int(2) 156 [10]=> 157 int(10) 158 [45]=> 159 int(45) 160} 161-- Iteration 8 -- 162bool(true) 163array(3) { 164 [2]=> 165 int(2) 166 [10]=> 167 int(10) 168 [45]=> 169 int(45) 170} 171-- Iteration 9 -- 172bool(true) 173array(3) { 174 [2]=> 175 int(2) 176 [10]=> 177 int(10) 178 [45]=> 179 int(45) 180} 181-- Iteration 10 -- 182bool(true) 183array(3) { 184 [2]=> 185 int(2) 186 [10]=> 187 int(10) 188 [45]=> 189 int(45) 190} 191-- Iteration 11 -- 192bool(true) 193array(3) { 194 [2]=> 195 int(2) 196 [10]=> 197 int(10) 198 [45]=> 199 int(45) 200} 201-- Iteration 12 -- 202bool(true) 203array(3) { 204 [2]=> 205 int(2) 206 [10]=> 207 int(10) 208 [45]=> 209 int(45) 210} 211-- Iteration 13 -- 212 213Warning: ksort() expects parameter 2 to be integer, string given in %s on line %d 214bool(false) 215array(3) { 216 [10]=> 217 int(10) 218 [2]=> 219 int(2) 220 [45]=> 221 int(45) 222} 223-- Iteration 14 -- 224 225Warning: ksort() expects parameter 2 to be integer, string given in %s on line %d 226bool(false) 227array(3) { 228 [10]=> 229 int(10) 230 [2]=> 231 int(2) 232 [45]=> 233 int(45) 234} 235-- Iteration 15 -- 236 237Warning: ksort() expects parameter 2 to be integer, string given in %s on line %d 238bool(false) 239array(3) { 240 [10]=> 241 int(10) 242 [2]=> 243 int(2) 244 [45]=> 245 int(45) 246} 247-- Iteration 16 -- 248 249Warning: ksort() expects parameter 2 to be integer, string given in %s on line %d 250bool(false) 251array(3) { 252 [10]=> 253 int(10) 254 [2]=> 255 int(2) 256 [45]=> 257 int(45) 258} 259-- Iteration 17 -- 260 261Warning: ksort() expects parameter 2 to be integer, object given in %s on line %d 262bool(false) 263array(3) { 264 [10]=> 265 int(10) 266 [2]=> 267 int(2) 268 [45]=> 269 int(45) 270} 271-- Iteration 18 -- 272 273Warning: ksort() expects parameter 2 to be integer, string given in %s on line %d 274bool(false) 275array(3) { 276 [10]=> 277 int(10) 278 [2]=> 279 int(2) 280 [45]=> 281 int(45) 282} 283-- Iteration 19 -- 284 285Warning: ksort() expects parameter 2 to be integer, string given in %s on line %d 286bool(false) 287array(3) { 288 [10]=> 289 int(10) 290 [2]=> 291 int(2) 292 [45]=> 293 int(45) 294} 295-- Iteration 20 -- 296 297Warning: ksort() expects parameter 2 to be integer, resource given in %s on line %d 298bool(false) 299array(3) { 300 [10]=> 301 int(10) 302 [2]=> 303 int(2) 304 [45]=> 305 int(45) 306} 307Done 308