1--TEST-- 2Test ksort() function : usage variations - sort strings 3--SKIPIF-- 4<?php 5if (substr(PHP_OS, 0, 3) == 'WIN') { 6 die("skip Output tested contains chars that are not shown the same on windows concole (ESC and co)"); 7} 8--FILE-- 9<?php 10/* Prototype : bool ksort ( array &$array [, int $sort_flags] ) 11 * Description: Sort an array by key, maintaining key to data correlation 12 * Source code: ext/standard/array.c 13*/ 14 15/* 16 * testing ksort() by providing array of string values for $array argument with 17 * following flag values: 18 * 1.flag value as defualt 19 * 2.SORT_REGULAR - compare items normally 20 * 3.SORT_STRING - compare items as strings 21*/ 22 23echo "*** Testing ksort() : usage variations ***\n"; 24 25$various_arrays = array ( 26 // diff. escape sequence chars with key values 27 array ( null => null, NULL => NULL, "\a" => "\a", "\cx" => "\cx", "\e" => "\e", 28 "\f" => "\f", "\n" =>"\n", "\r" => "\r", "\t" => "\t", "\xhh" => "\xhh", 29 "\ddd" => "\ddd", "\v" => "\v" 30 ), 31 32 // array containing different strings with key values 33 array ( 'Lemon' => "lemoN", 'o' => "Orange", 'B' => "banana", 'Apple' => "apple", 'te' => "Test", 34 't' => "TTTT", 'T' => "ttt", 'W' => "ww", 'X' => "x", 'x' => "X", 'O' => "oraNGe", 35 'B' => "BANANA" 36 ) 37); 38 39$flags = array("SORT_REGULAR" => SORT_REGULAR, "SORT_STRING" => SORT_STRING); 40 41$count = 1; 42echo "\n-- Testing ksort() by supplying various string arrays --\n"; 43 44// loop through to test ksort() with different arrays 45foreach ($various_arrays as $array) { 46 echo "\n-- Iteration $count --\n"; 47 48 echo "- With defualt sort flag -\n"; 49 $temp_array = $array; 50 var_dump(ksort($temp_array) ); // expecting : bool(true) 51 var_dump($temp_array); 52 53 // loop through $flags array and call ksort() with all possible sort flag values 54 foreach($flags as $key => $flag){ 55 echo "- Sort flag = $key -\n"; 56 $temp_array = $array; 57 var_dump(ksort($temp_array, $flag) ); // expecting : bool(true) 58 var_dump($temp_array); 59 } 60 $count++; 61} 62 63echo "Done\n"; 64?> 65--EXPECTF-- 66*** Testing ksort() : usage variations *** 67 68-- Testing ksort() by supplying various string arrays -- 69 70-- Iteration 1 -- 71- With defualt sort flag - 72bool(true) 73array(11) { 74 [""]=> 75 NULL 76 [" "]=> 77 string(1) " " 78 [" 79"]=> 80 string(1) " 81" 82 [""]=> 83 string(1) "" 84 [""]=> 85 string(1) "" 86 [" 86"]=> 87 string(1) " 87" 88 [""]=> 89 string(1) "" 90 ["\a"]=> 91 string(2) "\a" 92 ["\cx"]=> 93 string(3) "\cx" 94 ["\ddd"]=> 95 string(4) "\ddd" 96 ["\xhh"]=> 97 string(4) "\xhh" 98} 99- Sort flag = SORT_REGULAR - 100bool(true) 101array(11) { 102 [""]=> 103 NULL 104 [" "]=> 105 string(1) " " 106 [" 107"]=> 108 string(1) " 109" 110 [""]=> 111 string(1) "" 112 [""]=> 113 string(1) "" 114 [" 114"]=> 115 string(1) " 115" 116 [""]=> 117 string(1) "" 118 ["\a"]=> 119 string(2) "\a" 120 ["\cx"]=> 121 string(3) "\cx" 122 ["\ddd"]=> 123 string(4) "\ddd" 124 ["\xhh"]=> 125 string(4) "\xhh" 126} 127- Sort flag = SORT_STRING - 128bool(true) 129array(11) { 130 [""]=> 131 NULL 132 [" "]=> 133 string(1) " " 134 [" 135"]=> 136 string(1) " 137" 138 [""]=> 139 string(1) "" 140 [""]=> 141 string(1) "" 142 [" 142"]=> 143 string(1) " 143" 144 [""]=> 145 string(1) "" 146 ["\a"]=> 147 string(2) "\a" 148 ["\cx"]=> 149 string(3) "\cx" 150 ["\ddd"]=> 151 string(4) "\ddd" 152 ["\xhh"]=> 153 string(4) "\xhh" 154} 155 156-- Iteration 2 -- 157- With defualt sort flag - 158bool(true) 159array(11) { 160 ["Apple"]=> 161 string(5) "apple" 162 ["B"]=> 163 string(6) "BANANA" 164 ["Lemon"]=> 165 string(5) "lemoN" 166 ["O"]=> 167 string(6) "oraNGe" 168 ["T"]=> 169 string(3) "ttt" 170 ["W"]=> 171 string(2) "ww" 172 ["X"]=> 173 string(1) "x" 174 ["o"]=> 175 string(6) "Orange" 176 ["t"]=> 177 string(4) "TTTT" 178 ["te"]=> 179 string(4) "Test" 180 ["x"]=> 181 string(1) "X" 182} 183- Sort flag = SORT_REGULAR - 184bool(true) 185array(11) { 186 ["Apple"]=> 187 string(5) "apple" 188 ["B"]=> 189 string(6) "BANANA" 190 ["Lemon"]=> 191 string(5) "lemoN" 192 ["O"]=> 193 string(6) "oraNGe" 194 ["T"]=> 195 string(3) "ttt" 196 ["W"]=> 197 string(2) "ww" 198 ["X"]=> 199 string(1) "x" 200 ["o"]=> 201 string(6) "Orange" 202 ["t"]=> 203 string(4) "TTTT" 204 ["te"]=> 205 string(4) "Test" 206 ["x"]=> 207 string(1) "X" 208} 209- Sort flag = SORT_STRING - 210bool(true) 211array(11) { 212 ["Apple"]=> 213 string(5) "apple" 214 ["B"]=> 215 string(6) "BANANA" 216 ["Lemon"]=> 217 string(5) "lemoN" 218 ["O"]=> 219 string(6) "oraNGe" 220 ["T"]=> 221 string(3) "ttt" 222 ["W"]=> 223 string(2) "ww" 224 ["X"]=> 225 string(1) "x" 226 ["o"]=> 227 string(6) "Orange" 228 ["t"]=> 229 string(4) "TTTT" 230 ["te"]=> 231 string(4) "Test" 232 ["x"]=> 233 string(1) "X" 234} 235Done 236