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