1--TEST--
2Test krsort() 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 krsort ( array &$array [, int $sort_flags] )
11 * Description: Sort an array by key in reverse order, maintaining key to data correlation
12 * Source code: ext/standard/array.c
13*/
14
15/*
16 * testing krsort() 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
23echo "*** Testing krsort() : 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;
42echo "\n-- Testing krsort() by supplying various string arrays --\n";
43
44// loop through to test krsort() with different arrays
45foreach ($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(krsort($temp_array) ); // expecting : bool(true)
51  var_dump($temp_array);
52
53  // loop through $flags array and call krsort() 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(krsort($temp_array, $flag) ); // expecting : bool(true)
58    var_dump($temp_array);
59  }
60  $count++;
61}
62
63echo "Done\n";
64?>
65--EXPECT--
66*** Testing krsort() : usage variations ***
67
68-- Testing krsort() by supplying various string arrays --
69
70-- Iteration 1 --
71- With defualt sort flag -
72bool(true)
73array(11) {
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  [""]=>
97  NULL
98}
99- Sort flag = SORT_REGULAR -
100bool(true)
101array(11) {
102  ["\xhh"]=>
103  string(4) "\xhh"
104  ["\ddd"]=>
105  string(4) "\ddd"
106  ["\cx"]=>
107  string(3) "\cx"
108  ["\a"]=>
109  string(2) "\a"
110  [""]=>
111  string(1) ""
112  ["
112"]=>
113  string(1) "
113"
114  [""]=>
115  string(1) ""
116  [""]=>
117  string(1) ""
118  ["
119"]=>
120  string(1) "
121"
122  ["	"]=>
123  string(1) "	"
124  [""]=>
125  NULL
126}
127- Sort flag = SORT_STRING -
128bool(true)
129array(11) {
130  ["\xhh"]=>
131  string(4) "\xhh"
132  ["\ddd"]=>
133  string(4) "\ddd"
134  ["\cx"]=>
135  string(3) "\cx"
136  ["\a"]=>
137  string(2) "\a"
138  [""]=>
139  string(1) ""
140  ["
140"]=>
141  string(1) "
141"
142  [""]=>
143  string(1) ""
144  [""]=>
145  string(1) ""
146  ["
147"]=>
148  string(1) "
149"
150  ["	"]=>
151  string(1) "	"
152  [""]=>
153  NULL
154}
155
156-- Iteration 2 --
157- With defualt sort flag -
158bool(true)
159array(11) {
160  ["x"]=>
161  string(1) "X"
162  ["te"]=>
163  string(4) "Test"
164  ["t"]=>
165  string(4) "TTTT"
166  ["o"]=>
167  string(6) "Orange"
168  ["X"]=>
169  string(1) "x"
170  ["W"]=>
171  string(2) "ww"
172  ["T"]=>
173  string(3) "ttt"
174  ["O"]=>
175  string(6) "oraNGe"
176  ["Lemon"]=>
177  string(5) "lemoN"
178  ["B"]=>
179  string(6) "BANANA"
180  ["Apple"]=>
181  string(5) "apple"
182}
183- Sort flag = SORT_REGULAR -
184bool(true)
185array(11) {
186  ["x"]=>
187  string(1) "X"
188  ["te"]=>
189  string(4) "Test"
190  ["t"]=>
191  string(4) "TTTT"
192  ["o"]=>
193  string(6) "Orange"
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  ["Lemon"]=>
203  string(5) "lemoN"
204  ["B"]=>
205  string(6) "BANANA"
206  ["Apple"]=>
207  string(5) "apple"
208}
209- Sort flag = SORT_STRING -
210bool(true)
211array(11) {
212  ["x"]=>
213  string(1) "X"
214  ["te"]=>
215  string(4) "Test"
216  ["t"]=>
217  string(4) "TTTT"
218  ["o"]=>
219  string(6) "Orange"
220  ["X"]=>
221  string(1) "x"
222  ["W"]=>
223  string(2) "ww"
224  ["T"]=>
225  string(3) "ttt"
226  ["O"]=>
227  string(6) "oraNGe"
228  ["Lemon"]=>
229  string(5) "lemoN"
230  ["B"]=>
231  string(6) "BANANA"
232  ["Apple"]=>
233  string(5) "apple"
234}
235Done
236