1--TEST--
2Test arsort() function : basic functionality
3--FILE--
4<?php
5/* Prototype  : bool arsort ( array &$array [, int $sort_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 integer/string arrays to check the basic functionality
13 * with following flag values.
14 *  flag value as default
15 *  SORT_REGULAR - compare items normally
16 *  SORT_NUMERIC - compare items numerically
17 *  SORT_STRING - compare items as strings
18*/
19
20echo "*** Testing arsort() : basic functionality ***\n";
21
22// an array containing unsorted string values with indices
23$unsorted_strings = array(
24	"l" => "lemon", "o" => "orange",
25	"O" => "Orange", "O1" => "Orange1", "o2" => "orange2", "O3" => "Orange3", "o20" => "orange20",
26	"b" => "banana",
27);
28// an array containing unsorted numeric values with indices
29$unsorted_numerics =  array( 1 => 100, 2 => 33, 3 => 555, 4 => 22 );
30
31echo "\n-- Testing arsort() by supplying string array, 'flag' value is default --\n";
32$temp_array = $unsorted_strings;
33var_dump( arsort($temp_array) ); // expecting : bool(true)
34var_dump( $temp_array);
35
36echo "\n-- Testing arsort() by supplying numeric array, 'flag' value is default --\n";
37$temp_array = $unsorted_numerics;
38var_dump( arsort($temp_array) ); // expecting : bool(true)
39var_dump( $temp_array);
40
41echo "\n-- Testing arsort() by supplying string array, 'flag' = SORT_REGULAR --\n";
42$temp_array = $unsorted_strings;
43var_dump( arsort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
44var_dump( $temp_array);
45
46echo "\n-- Testing arsort() by supplying numeric array, 'flag' = SORT_REGULAR --\n";
47$temp_array = $unsorted_numerics;
48var_dump( arsort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
49var_dump( $temp_array);
50
51echo "\n-- Testing arsort() by supplying string array, 'flag' = SORT_STRING --\n";
52$temp_array = $unsorted_strings;
53var_dump( arsort($temp_array, SORT_STRING) ); // expecting : bool(true)
54var_dump( $temp_array);
55
56echo "\n-- Testing arsort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --\n";
57$temp_array = $unsorted_strings;
58var_dump( arsort($temp_array, SORT_STRING|SORT_FLAG_CASE) ); // expecting : bool(true)
59var_dump( $temp_array);
60
61echo "\n-- Testing arsort() by supplying string array (natural), 'flag' = SORT_NATURAL --\n";
62$temp_array = $unsorted_strings;
63var_dump( arsort($temp_array, SORT_NATURAL) ); // expecting : bool(true)
64var_dump( $temp_array);
65
66echo "\n-- Testing arsort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --\n";
67$temp_array = $unsorted_strings;
68var_dump( arsort($temp_array, SORT_NATURAL|SORT_FLAG_CASE) ); // expecting : bool(true)
69var_dump( $temp_array);
70
71echo "\n-- Testing arsort() by supplying numeric array, 'flag' = SORT_NUMERIC --\n";
72$temp_array = $unsorted_numerics;
73var_dump( arsort($temp_array, SORT_NUMERIC) ); // expecting : bool(true)
74var_dump( $temp_array);
75
76echo "Done\n";
77?>
78--EXPECT--
79*** Testing arsort() : basic functionality ***
80
81-- Testing arsort() by supplying string array, 'flag' value is default --
82bool(true)
83array(8) {
84  ["o20"]=>
85  string(8) "orange20"
86  ["o2"]=>
87  string(7) "orange2"
88  ["o"]=>
89  string(6) "orange"
90  ["l"]=>
91  string(5) "lemon"
92  ["b"]=>
93  string(6) "banana"
94  ["O3"]=>
95  string(7) "Orange3"
96  ["O1"]=>
97  string(7) "Orange1"
98  ["O"]=>
99  string(6) "Orange"
100}
101
102-- Testing arsort() by supplying numeric array, 'flag' value is default --
103bool(true)
104array(4) {
105  [3]=>
106  int(555)
107  [1]=>
108  int(100)
109  [2]=>
110  int(33)
111  [4]=>
112  int(22)
113}
114
115-- Testing arsort() by supplying string array, 'flag' = SORT_REGULAR --
116bool(true)
117array(8) {
118  ["o20"]=>
119  string(8) "orange20"
120  ["o2"]=>
121  string(7) "orange2"
122  ["o"]=>
123  string(6) "orange"
124  ["l"]=>
125  string(5) "lemon"
126  ["b"]=>
127  string(6) "banana"
128  ["O3"]=>
129  string(7) "Orange3"
130  ["O1"]=>
131  string(7) "Orange1"
132  ["O"]=>
133  string(6) "Orange"
134}
135
136-- Testing arsort() by supplying numeric array, 'flag' = SORT_REGULAR --
137bool(true)
138array(4) {
139  [3]=>
140  int(555)
141  [1]=>
142  int(100)
143  [2]=>
144  int(33)
145  [4]=>
146  int(22)
147}
148
149-- Testing arsort() by supplying string array, 'flag' = SORT_STRING --
150bool(true)
151array(8) {
152  ["o20"]=>
153  string(8) "orange20"
154  ["o2"]=>
155  string(7) "orange2"
156  ["o"]=>
157  string(6) "orange"
158  ["l"]=>
159  string(5) "lemon"
160  ["b"]=>
161  string(6) "banana"
162  ["O3"]=>
163  string(7) "Orange3"
164  ["O1"]=>
165  string(7) "Orange1"
166  ["O"]=>
167  string(6) "Orange"
168}
169
170-- Testing arsort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --
171bool(true)
172array(8) {
173  ["O3"]=>
174  string(7) "Orange3"
175  ["o20"]=>
176  string(8) "orange20"
177  ["o2"]=>
178  string(7) "orange2"
179  ["O1"]=>
180  string(7) "Orange1"
181  ["o"]=>
182  string(6) "orange"
183  ["O"]=>
184  string(6) "Orange"
185  ["l"]=>
186  string(5) "lemon"
187  ["b"]=>
188  string(6) "banana"
189}
190
191-- Testing arsort() by supplying string array (natural), 'flag' = SORT_NATURAL --
192bool(true)
193array(8) {
194  ["o20"]=>
195  string(8) "orange20"
196  ["o2"]=>
197  string(7) "orange2"
198  ["o"]=>
199  string(6) "orange"
200  ["l"]=>
201  string(5) "lemon"
202  ["b"]=>
203  string(6) "banana"
204  ["O3"]=>
205  string(7) "Orange3"
206  ["O1"]=>
207  string(7) "Orange1"
208  ["O"]=>
209  string(6) "Orange"
210}
211
212-- Testing arsort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --
213bool(true)
214array(8) {
215  ["o20"]=>
216  string(8) "orange20"
217  ["O3"]=>
218  string(7) "Orange3"
219  ["o2"]=>
220  string(7) "orange2"
221  ["O1"]=>
222  string(7) "Orange1"
223  ["o"]=>
224  string(6) "orange"
225  ["O"]=>
226  string(6) "Orange"
227  ["l"]=>
228  string(5) "lemon"
229  ["b"]=>
230  string(6) "banana"
231}
232
233-- Testing arsort() by supplying numeric array, 'flag' = SORT_NUMERIC --
234bool(true)
235array(4) {
236  [3]=>
237  int(555)
238  [1]=>
239  int(100)
240  [2]=>
241  int(33)
242  [4]=>
243  int(22)
244}
245Done
246