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