Return to ksort_variation5.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test ksort() function : usage variations - sort strings 3: --SKIPIF-- 4: <?php 5: if (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: 23: echo "*** 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; 42: echo "\n-- Testing ksort() by supplying various string arrays --\n"; 43: 44: // loop through to test ksort() with different arrays 45: foreach ($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: 63: echo "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 - 72: bool(true) 73: array(11) { 74: [""]=> 75: NULL 76: [" "]=> 77: string(1) " " 78: [" 79: "]=> 80: string(1) " 81: " 82: [""]=> 83: string(1) "" 84: [""]=> 85: string(1) "" 86: [" 87: "]=> 88: string(1) " 89: " 90: ["\a"]=> 91: string(2) "\a" 92: ["\cx"]=> 93: string(3) "\cx" 94: ["\ddd"]=> 95: string(4) "\ddd" 96: ["\e"]=> 97: string(2) "\e" 98: ["\xhh"]=> 99: string(4) "\xhh" 100: } 101: - Sort flag = SORT_REGULAR - 102: bool(true) 103: array(11) { 104: [""]=> 105: NULL 106: [" "]=> 107: string(1) " " 108: [" 109: "]=> 110: string(1) " 111: " 112: [""]=> 113: string(1) "" 114: [""]=> 115: string(1) "" 116: [" 117: "]=> 118: string(1) " 119: " 120: ["\a"]=> 121: string(2) "\a" 122: ["\cx"]=> 123: string(3) "\cx" 124: ["\ddd"]=> 125: string(4) "\ddd" 126: ["\e"]=> 127: string(2) "\e" 128: ["\xhh"]=> 129: string(4) "\xhh" 130: } 131: - Sort flag = SORT_STRING - 132: bool(true) 133: array(11) { 134: [""]=> 135: NULL 136: [" "]=> 137: string(1) " " 138: [" 139: "]=> 140: string(1) " 141: " 142: [""]=> 143: string(1) "" 144: [""]=> 145: string(1) "" 146: [" 147: "]=> 148: string(1) " 149: " 150: ["\a"]=> 151: string(2) "\a" 152: ["\cx"]=> 153: string(3) "\cx" 154: ["\ddd"]=> 155: string(4) "\ddd" 156: ["\e"]=> 157: string(2) "\e" 158: ["\xhh"]=> 159: string(4) "\xhh" 160: } 161: 162: -- Iteration 2 -- 163: - With defualt sort flag - 164: bool(true) 165: array(11) { 166: ["Apple"]=> 167: string(5) "apple" 168: ["B"]=> 169: string(6) "BANANA" 170: ["Lemon"]=> 171: string(5) "lemoN" 172: ["O"]=> 173: string(6) "oraNGe" 174: ["T"]=> 175: string(3) "ttt" 176: ["W"]=> 177: string(2) "ww" 178: ["X"]=> 179: string(1) "x" 180: ["o"]=> 181: string(6) "Orange" 182: ["t"]=> 183: string(4) "TTTT" 184: ["te"]=> 185: string(4) "Test" 186: ["x"]=> 187: string(1) "X" 188: } 189: - Sort flag = SORT_REGULAR - 190: bool(true) 191: array(11) { 192: ["Apple"]=> 193: string(5) "apple" 194: ["B"]=> 195: string(6) "BANANA" 196: ["Lemon"]=> 197: string(5) "lemoN" 198: ["O"]=> 199: string(6) "oraNGe" 200: ["T"]=> 201: string(3) "ttt" 202: ["W"]=> 203: string(2) "ww" 204: ["X"]=> 205: string(1) "x" 206: ["o"]=> 207: string(6) "Orange" 208: ["t"]=> 209: string(4) "TTTT" 210: ["te"]=> 211: string(4) "Test" 212: ["x"]=> 213: string(1) "X" 214: } 215: - Sort flag = SORT_STRING - 216: bool(true) 217: array(11) { 218: ["Apple"]=> 219: string(5) "apple" 220: ["B"]=> 221: string(6) "BANANA" 222: ["Lemon"]=> 223: string(5) "lemoN" 224: ["O"]=> 225: string(6) "oraNGe" 226: ["T"]=> 227: string(3) "ttt" 228: ["W"]=> 229: string(2) "ww" 230: ["X"]=> 231: string(1) "x" 232: ["o"]=> 233: string(6) "Orange" 234: ["t"]=> 235: string(4) "TTTT" 236: ["te"]=> 237: string(4) "Test" 238: ["x"]=> 239: string(1) "X" 240: } 241: Done