1--TEST--
2Test asort() function : usage variations - sorting arrays with/without keys, 'sort_flags' as default/SORT_REGULAR
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 arrays  with key values for $array argument with following flag values.
13 * 1.flag value as default
14 * 2.SORT_REGULAR - compare items normally
15 */
16
17echo "*** Testing asort() : usage variations ***\n";
18
19// list of arrays with/without key values
20$various_arrays = array (
21  array(5 => 55,  66, 22, 33, 11),
22  array ("a" => "orange",  "banana", "c" => "apple"),
23  array(1, 2, 3, 4, 5, 6),
24  array("first", 5 => "second", "third"),
25  array(1, 1, 8 => 1,  4 => 1, 19, 3 => 13),
26  array('bar' => 'baz', "foo" => 1),
27  array('a'=>1,'b'=>array('e'=>2,'f'=>3),'c'=>array('g'=>4),'d'=>5),
28);
29
30$count = 1;
31echo "\n-- Testing asort() by supplying various arrays with key values --\n";
32
33// loop through to test asort() with different arrays,
34// to test the new keys for the elements in the sorted array
35foreach ($various_arrays as $array) {
36  echo "\n-- Iteration $count --\n";
37
38  echo "- With default sort_flag -\n";
39  $temp_array = $array;
40  var_dump(asort($temp_array) );
41  var_dump($temp_array);
42
43  echo "- Sort_flag = SORT_REGULAR -\n";
44  $temp_array = $array;
45  var_dump(asort($temp_array, SORT_REGULAR) );
46  var_dump($temp_array);
47  $count++;
48}
49
50echo "Done\n";
51?>
52--EXPECT--
53*** Testing asort() : usage variations ***
54
55-- Testing asort() by supplying various arrays with key values --
56
57-- Iteration 1 --
58- With default sort_flag -
59bool(true)
60array(5) {
61  [9]=>
62  int(11)
63  [7]=>
64  int(22)
65  [8]=>
66  int(33)
67  [5]=>
68  int(55)
69  [6]=>
70  int(66)
71}
72- Sort_flag = SORT_REGULAR -
73bool(true)
74array(5) {
75  [9]=>
76  int(11)
77  [7]=>
78  int(22)
79  [8]=>
80  int(33)
81  [5]=>
82  int(55)
83  [6]=>
84  int(66)
85}
86
87-- Iteration 2 --
88- With default sort_flag -
89bool(true)
90array(3) {
91  ["c"]=>
92  string(5) "apple"
93  [0]=>
94  string(6) "banana"
95  ["a"]=>
96  string(6) "orange"
97}
98- Sort_flag = SORT_REGULAR -
99bool(true)
100array(3) {
101  ["c"]=>
102  string(5) "apple"
103  [0]=>
104  string(6) "banana"
105  ["a"]=>
106  string(6) "orange"
107}
108
109-- Iteration 3 --
110- With default sort_flag -
111bool(true)
112array(6) {
113  [0]=>
114  int(1)
115  [1]=>
116  int(2)
117  [2]=>
118  int(3)
119  [3]=>
120  int(4)
121  [4]=>
122  int(5)
123  [5]=>
124  int(6)
125}
126- Sort_flag = SORT_REGULAR -
127bool(true)
128array(6) {
129  [0]=>
130  int(1)
131  [1]=>
132  int(2)
133  [2]=>
134  int(3)
135  [3]=>
136  int(4)
137  [4]=>
138  int(5)
139  [5]=>
140  int(6)
141}
142
143-- Iteration 4 --
144- With default sort_flag -
145bool(true)
146array(3) {
147  [0]=>
148  string(5) "first"
149  [5]=>
150  string(6) "second"
151  [6]=>
152  string(5) "third"
153}
154- Sort_flag = SORT_REGULAR -
155bool(true)
156array(3) {
157  [0]=>
158  string(5) "first"
159  [5]=>
160  string(6) "second"
161  [6]=>
162  string(5) "third"
163}
164
165-- Iteration 5 --
166- With default sort_flag -
167bool(true)
168array(6) {
169  [0]=>
170  int(1)
171  [1]=>
172  int(1)
173  [8]=>
174  int(1)
175  [4]=>
176  int(1)
177  [3]=>
178  int(13)
179  [9]=>
180  int(19)
181}
182- Sort_flag = SORT_REGULAR -
183bool(true)
184array(6) {
185  [0]=>
186  int(1)
187  [1]=>
188  int(1)
189  [8]=>
190  int(1)
191  [4]=>
192  int(1)
193  [3]=>
194  int(13)
195  [9]=>
196  int(19)
197}
198
199-- Iteration 6 --
200- With default sort_flag -
201bool(true)
202array(2) {
203  ["bar"]=>
204  string(3) "baz"
205  ["foo"]=>
206  int(1)
207}
208- Sort_flag = SORT_REGULAR -
209bool(true)
210array(2) {
211  ["bar"]=>
212  string(3) "baz"
213  ["foo"]=>
214  int(1)
215}
216
217-- Iteration 7 --
218- With default sort_flag -
219bool(true)
220array(4) {
221  ["a"]=>
222  int(1)
223  ["d"]=>
224  int(5)
225  ["c"]=>
226  array(1) {
227    ["g"]=>
228    int(4)
229  }
230  ["b"]=>
231  array(2) {
232    ["e"]=>
233    int(2)
234    ["f"]=>
235    int(3)
236  }
237}
238- Sort_flag = SORT_REGULAR -
239bool(true)
240array(4) {
241  ["a"]=>
242  int(1)
243  ["d"]=>
244  int(5)
245  ["c"]=>
246  array(1) {
247    ["g"]=>
248    int(4)
249  }
250  ["b"]=>
251  array(2) {
252    ["e"]=>
253    int(2)
254    ["f"]=>
255    int(3)
256  }
257}
258Done
259