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