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