1--TEST-- 2Test asort() 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 asort ( array &$array [, int $asort_flags] ) 11 * Description: Sort an array and maintain index association 12 Elements will be arranged from lowest to highest when this function has completed. 13 * Source code: ext/standard/array.c 14*/ 15 16/* 17 * testing asort() by providing different string arrays for $array argument with following flag values 18 * flag value as defualt 19 * SORT_REGULAR - compare items normally 20 * SORT_STRING - compare items as strings 21*/ 22 23echo "*** Testing asort() : usage variations ***\n"; 24 25$various_arrays = array ( 26 // group of escape sequences 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 contains combination of capital/small letters 33 array ('l' => "lemoN", 'O' => "Orange", 'b' => "banana", 'a' => "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 asort() by supplying various string arrays --\n"; 43 44// loop through to test asort() with different arrays 45foreach ($various_arrays as $array) { 46 echo "\n-- Iteration $count --\n"; 47 48 echo "- With default sort_flag -\n"; 49 $temp_array = $array; 50 var_dump(asort($temp_array) ); // expecting : bool(true) 51 var_dump($temp_array); 52 53 // loop through $flags array and setting all possible flag values 54 foreach($flags as $key => $flag){ 55 echo "- Sort_flag = $key -\n"; 56 $temp_array = $array; 57 var_dump(asort($temp_array, $flag) ); // expecting : bool(true) 58 var_dump($temp_array); 59 } 60 $count++; 61} 62 63echo "Done\n"; 64?> 65--EXPECT-- 66*** Testing asort() : usage variations *** 67 68-- Testing asort() by supplying various string arrays -- 69 70-- Iteration 1 -- 71- With default sort_flag - 72bool(true) 73array(12) { 74 ["null"]=> 75 NULL 76 ["NULL"]=> 77 NULL 78 [" "]=> 79 string(1) " " 80 [" 81"]=> 82 string(1) " 83" 84 [""]=> 85 string(1) "" 86 [""]=> 87 string(1) "" 88 [" 88"]=> 89 string(1) " 89" 90 [""]=> 91 string(1) "" 92 ["\a"]=> 93 string(2) "\a" 94 ["\cx"]=> 95 string(3) "\cx" 96 ["\ddd"]=> 97 string(4) "\ddd" 98 ["\xhh"]=> 99 string(4) "\xhh" 100} 101- Sort_flag = SORT_REGULAR - 102bool(true) 103array(12) { 104 ["null"]=> 105 NULL 106 ["NULL"]=> 107 NULL 108 [" "]=> 109 string(1) " " 110 [" 111"]=> 112 string(1) " 113" 114 [""]=> 115 string(1) "" 116 [""]=> 117 string(1) "" 118 [" 118"]=> 119 string(1) " 119" 120 [""]=> 121 string(1) "" 122 ["\a"]=> 123 string(2) "\a" 124 ["\cx"]=> 125 string(3) "\cx" 126 ["\ddd"]=> 127 string(4) "\ddd" 128 ["\xhh"]=> 129 string(4) "\xhh" 130} 131- Sort_flag = SORT_STRING - 132bool(true) 133array(12) { 134 ["null"]=> 135 NULL 136 ["NULL"]=> 137 NULL 138 [" "]=> 139 string(1) " " 140 [" 141"]=> 142 string(1) " 143" 144 [""]=> 145 string(1) "" 146 [""]=> 147 string(1) "" 148 [" 148"]=> 149 string(1) " 149" 150 [""]=> 151 string(1) "" 152 ["\a"]=> 153 string(2) "\a" 154 ["\cx"]=> 155 string(3) "\cx" 156 ["\ddd"]=> 157 string(4) "\ddd" 158 ["\xhh"]=> 159 string(4) "\xhh" 160} 161 162-- Iteration 2 -- 163- With default sort_flag - 164bool(true) 165array(12) { 166 ["B"]=> 167 string(6) "BANANA" 168 ["O"]=> 169 string(6) "Orange" 170 ["T"]=> 171 string(4) "TTTT" 172 ["Te"]=> 173 string(4) "Test" 174 ["X"]=> 175 string(1) "X" 176 ["a"]=> 177 string(5) "apple" 178 ["b"]=> 179 string(6) "banana" 180 ["l"]=> 181 string(5) "lemoN" 182 ["o"]=> 183 string(6) "oraNGe" 184 ["t"]=> 185 string(3) "ttt" 186 ["w"]=> 187 string(2) "ww" 188 ["x"]=> 189 string(1) "x" 190} 191- Sort_flag = SORT_REGULAR - 192bool(true) 193array(12) { 194 ["B"]=> 195 string(6) "BANANA" 196 ["O"]=> 197 string(6) "Orange" 198 ["T"]=> 199 string(4) "TTTT" 200 ["Te"]=> 201 string(4) "Test" 202 ["X"]=> 203 string(1) "X" 204 ["a"]=> 205 string(5) "apple" 206 ["b"]=> 207 string(6) "banana" 208 ["l"]=> 209 string(5) "lemoN" 210 ["o"]=> 211 string(6) "oraNGe" 212 ["t"]=> 213 string(3) "ttt" 214 ["w"]=> 215 string(2) "ww" 216 ["x"]=> 217 string(1) "x" 218} 219- Sort_flag = SORT_STRING - 220bool(true) 221array(12) { 222 ["B"]=> 223 string(6) "BANANA" 224 ["O"]=> 225 string(6) "Orange" 226 ["T"]=> 227 string(4) "TTTT" 228 ["Te"]=> 229 string(4) "Test" 230 ["X"]=> 231 string(1) "X" 232 ["a"]=> 233 string(5) "apple" 234 ["b"]=> 235 string(6) "banana" 236 ["l"]=> 237 string(5) "lemoN" 238 ["o"]=> 239 string(6) "oraNGe" 240 ["t"]=> 241 string(3) "ttt" 242 ["w"]=> 243 string(2) "ww" 244 ["x"]=> 245 string(1) "x" 246} 247Done 248