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