xref: /PHP-5.3/ext/standard/tests/array/002.phpt (revision a541bb80)
1--TEST--
2Test arsort, asort, krsort, ksort, rsort, and sort
3--INI--
4precision=14
5--FILE--
6<?php
7require(dirname(__FILE__) . '/data.inc');
8
9function test_sort ($sort_function, $data) {
10    echo "\n -- Testing $sort_function() -- \n";
11	echo "No second argument:\n";
12    $sort_function ($data);
13    var_dump ($data);
14	echo "Using SORT_REGULAR:\n";
15    $sort_function ($data, SORT_REGULAR);
16    var_dump ($data);
17	echo "Using SORT_NUMERIC:\n";
18    $sort_function ($data, SORT_NUMERIC);
19    var_dump ($data);
20	echo "Using SORT_STRING\n";
21    $sort_function ($data, SORT_STRING);
22    var_dump ($data);
23}
24
25echo "Unsorted data:\n";
26var_dump ($data);
27foreach (array ('arsort', 'asort', 'krsort', 'ksort', 'rsort', 'sort') as $test_function) {
28    test_sort ($test_function, $data);
29}
30
31?>
32--EXPECTF--
33Unsorted data:
34array(8) {
35  [0]=>
36  string(3) "PHP"
37  [17]=>
38  string(27) "PHP: Hypertext Preprocessor"
39  [5]=>
40  string(4) "Test"
41  ["test"]=>
42  int(27)
43  [1000]=>
44  string(4) "test"
45  [-1000]=>
46  array(2) {
47    [0]=>
48    string(6) "banana"
49    [1]=>
50    string(6) "orange"
51  }
52  [1001]=>
53  string(6) "monkey"
54  [16777216]=>
55  float(-0.33333333333333)
56}
57
58 -- Testing arsort() --
59No second argument:
60array(8) {
61  [-1000]=>
62  array(2) {
63    [0]=>
64    string(6) "banana"
65    [1]=>
66    string(6) "orange"
67  }
68  ["test"]=>
69  int(27)
70  [1000]=>
71  string(4) "test"
72  [1001]=>
73  string(6) "monkey"
74  [5]=>
75  string(4) "Test"
76  [17]=>
77  string(27) "PHP: Hypertext Preprocessor"
78  [0]=>
79  string(3) "PHP"
80  [16777216]=>
81  float(-0.33333333333333)
82}
83Using SORT_REGULAR:
84array(8) {
85  [-1000]=>
86  array(2) {
87    [0]=>
88    string(6) "banana"
89    [1]=>
90    string(6) "orange"
91  }
92  ["test"]=>
93  int(27)
94  [1000]=>
95  string(4) "test"
96  [1001]=>
97  string(6) "monkey"
98  [5]=>
99  string(4) "Test"
100  [17]=>
101  string(27) "PHP: Hypertext Preprocessor"
102  [0]=>
103  string(3) "PHP"
104  [16777216]=>
105  float(-0.33333333333333)
106}
107Using SORT_NUMERIC:
108array(8) {
109  ["test"]=>
110  int(27)
111  [-1000]=>
112  array(2) {
113    [0]=>
114    string(6) "banana"
115    [1]=>
116    string(6) "orange"
117  }
118  [0]=>
119  string(3) "PHP"
120  [17]=>
121  string(27) "PHP: Hypertext Preprocessor"
122  [1001]=>
123  string(6) "monkey"
124  [5]=>
125  string(4) "Test"
126  [1000]=>
127  string(4) "test"
128  [16777216]=>
129  float(-0.33333333333333)
130}
131Using SORT_STRING
132array(8) {
133  [1000]=>
134  string(4) "test"
135  [1001]=>
136  string(6) "monkey"
137  [5]=>
138  string(4) "Test"
139  [17]=>
140  string(27) "PHP: Hypertext Preprocessor"
141  [0]=>
142  string(3) "PHP"
143  [-1000]=>
144  array(2) {
145    [0]=>
146    string(6) "banana"
147    [1]=>
148    string(6) "orange"
149  }
150  ["test"]=>
151  int(27)
152  [16777216]=>
153  float(-0.33333333333333)
154}
155
156 -- Testing asort() --
157No second argument:
158array(8) {
159  [16777216]=>
160  float(-0.33333333333333)
161  [0]=>
162  string(3) "PHP"
163  [17]=>
164  string(27) "PHP: Hypertext Preprocessor"
165  [5]=>
166  string(4) "Test"
167  [1001]=>
168  string(6) "monkey"
169  [1000]=>
170  string(4) "test"
171  ["test"]=>
172  int(27)
173  [-1000]=>
174  array(2) {
175    [0]=>
176    string(6) "banana"
177    [1]=>
178    string(6) "orange"
179  }
180}
181Using SORT_REGULAR:
182array(8) {
183  [16777216]=>
184  float(-0.33333333333333)
185  [0]=>
186  string(3) "PHP"
187  [17]=>
188  string(27) "PHP: Hypertext Preprocessor"
189  [5]=>
190  string(4) "Test"
191  [1001]=>
192  string(6) "monkey"
193  [1000]=>
194  string(4) "test"
195  ["test"]=>
196  int(27)
197  [-1000]=>
198  array(2) {
199    [0]=>
200    string(6) "banana"
201    [1]=>
202    string(6) "orange"
203  }
204}
205Using SORT_NUMERIC:
206array(8) {
207  [16777216]=>
208  float(-0.33333333333333)
209  [1001]=>
210  string(6) "monkey"
211  [1000]=>
212  string(4) "test"
213  [5]=>
214  string(4) "Test"
215  [17]=>
216  string(27) "PHP: Hypertext Preprocessor"
217  [0]=>
218  string(3) "PHP"
219  [-1000]=>
220  array(2) {
221    [0]=>
222    string(6) "banana"
223    [1]=>
224    string(6) "orange"
225  }
226  ["test"]=>
227  int(27)
228}
229Using SORT_STRING
230array(8) {
231  [16777216]=>
232  float(-0.33333333333333)
233  ["test"]=>
234  int(27)
235  [-1000]=>
236  array(2) {
237    [0]=>
238    string(6) "banana"
239    [1]=>
240    string(6) "orange"
241  }
242  [0]=>
243  string(3) "PHP"
244  [17]=>
245  string(27) "PHP: Hypertext Preprocessor"
246  [5]=>
247  string(4) "Test"
248  [1001]=>
249  string(6) "monkey"
250  [1000]=>
251  string(4) "test"
252}
253
254 -- Testing krsort() --
255No second argument:
256array(8) {
257  [16777216]=>
258  float(-0.33333333333333)
259  [1001]=>
260  string(6) "monkey"
261  [1000]=>
262  string(4) "test"
263  [17]=>
264  string(27) "PHP: Hypertext Preprocessor"
265  [5]=>
266  string(4) "Test"
267  ["test"]=>
268  int(27)
269  [0]=>
270  string(3) "PHP"
271  [-1000]=>
272  array(2) {
273    [0]=>
274    string(6) "banana"
275    [1]=>
276    string(6) "orange"
277  }
278}
279Using SORT_REGULAR:
280array(8) {
281  [16777216]=>
282  float(-0.33333333333333)
283  [1001]=>
284  string(6) "monkey"
285  [1000]=>
286  string(4) "test"
287  [17]=>
288  string(27) "PHP: Hypertext Preprocessor"
289  [5]=>
290  string(4) "Test"
291  [0]=>
292  string(3) "PHP"
293  ["test"]=>
294  int(27)
295  [-1000]=>
296  array(2) {
297    [0]=>
298    string(6) "banana"
299    [1]=>
300    string(6) "orange"
301  }
302}
303Using SORT_NUMERIC:
304array(8) {
305  [16777216]=>
306  float(-0.33333333333333)
307  [1001]=>
308  string(6) "monkey"
309  [1000]=>
310  string(4) "test"
311  [17]=>
312  string(27) "PHP: Hypertext Preprocessor"
313  [5]=>
314  string(4) "Test"
315  ["test"]=>
316  int(27)
317  [0]=>
318  string(3) "PHP"
319  [-1000]=>
320  array(2) {
321    [0]=>
322    string(6) "banana"
323    [1]=>
324    string(6) "orange"
325  }
326}
327Using SORT_STRING
328array(8) {
329  ["test"]=>
330  int(27)
331  [5]=>
332  string(4) "Test"
333  [17]=>
334  string(27) "PHP: Hypertext Preprocessor"
335  [16777216]=>
336  float(-0.33333333333333)
337  [1001]=>
338  string(6) "monkey"
339  [1000]=>
340  string(4) "test"
341  [0]=>
342  string(3) "PHP"
343  [-1000]=>
344  array(2) {
345    [0]=>
346    string(6) "banana"
347    [1]=>
348    string(6) "orange"
349  }
350}
351
352 -- Testing ksort() --
353No second argument:
354array(8) {
355  [-1000]=>
356  array(2) {
357    [0]=>
358    string(6) "banana"
359    [1]=>
360    string(6) "orange"
361  }
362  [0]=>
363  string(3) "PHP"
364  ["test"]=>
365  int(27)
366  [5]=>
367  string(4) "Test"
368  [17]=>
369  string(27) "PHP: Hypertext Preprocessor"
370  [1000]=>
371  string(4) "test"
372  [1001]=>
373  string(6) "monkey"
374  [16777216]=>
375  float(-0.33333333333333)
376}
377Using SORT_REGULAR:
378array(8) {
379  [-1000]=>
380  array(2) {
381    [0]=>
382    string(6) "banana"
383    [1]=>
384    string(6) "orange"
385  }
386  ["test"]=>
387  int(27)
388  [0]=>
389  string(3) "PHP"
390  [5]=>
391  string(4) "Test"
392  [17]=>
393  string(27) "PHP: Hypertext Preprocessor"
394  [1000]=>
395  string(4) "test"
396  [1001]=>
397  string(6) "monkey"
398  [16777216]=>
399  float(-0.33333333333333)
400}
401Using SORT_NUMERIC:
402array(8) {
403  [-1000]=>
404  array(2) {
405    [0]=>
406    string(6) "banana"
407    [1]=>
408    string(6) "orange"
409  }
410  [0]=>
411  string(3) "PHP"
412  ["test"]=>
413  int(27)
414  [5]=>
415  string(4) "Test"
416  [17]=>
417  string(27) "PHP: Hypertext Preprocessor"
418  [1000]=>
419  string(4) "test"
420  [1001]=>
421  string(6) "monkey"
422  [16777216]=>
423  float(-0.33333333333333)
424}
425Using SORT_STRING
426array(8) {
427  [-1000]=>
428  array(2) {
429    [0]=>
430    string(6) "banana"
431    [1]=>
432    string(6) "orange"
433  }
434  [0]=>
435  string(3) "PHP"
436  [1000]=>
437  string(4) "test"
438  [1001]=>
439  string(6) "monkey"
440  [16777216]=>
441  float(-0.33333333333333)
442  [17]=>
443  string(27) "PHP: Hypertext Preprocessor"
444  [5]=>
445  string(4) "Test"
446  ["test"]=>
447  int(27)
448}
449
450 -- Testing rsort() --
451No second argument:
452array(8) {
453  [0]=>
454  array(2) {
455    [0]=>
456    string(6) "banana"
457    [1]=>
458    string(6) "orange"
459  }
460  [1]=>
461  int(27)
462  [2]=>
463  string(4) "test"
464  [3]=>
465  string(6) "monkey"
466  [4]=>
467  string(4) "Test"
468  [5]=>
469  string(27) "PHP: Hypertext Preprocessor"
470  [6]=>
471  string(3) "PHP"
472  [7]=>
473  float(-0.33333333333333)
474}
475Using SORT_REGULAR:
476array(8) {
477  [0]=>
478  array(2) {
479    [0]=>
480    string(6) "banana"
481    [1]=>
482    string(6) "orange"
483  }
484  [1]=>
485  int(27)
486  [2]=>
487  string(4) "test"
488  [3]=>
489  string(6) "monkey"
490  [4]=>
491  string(4) "Test"
492  [5]=>
493  string(27) "PHP: Hypertext Preprocessor"
494  [6]=>
495  string(3) "PHP"
496  [7]=>
497  float(-0.33333333333333)
498}
499Using SORT_NUMERIC:
500array(8) {
501  [0]=>
502  int(27)
503  [1]=>
504  array(2) {
505    [0]=>
506    string(6) "banana"
507    [1]=>
508    string(6) "orange"
509  }
510  [2]=>
511  string(3) "PHP"
512  [3]=>
513  string(27) "PHP: Hypertext Preprocessor"
514  [4]=>
515  string(6) "monkey"
516  [5]=>
517  string(4) "Test"
518  [6]=>
519  string(4) "test"
520  [7]=>
521  float(-0.33333333333333)
522}
523Using SORT_STRING
524array(8) {
525  [0]=>
526  string(4) "test"
527  [1]=>
528  string(6) "monkey"
529  [2]=>
530  string(4) "Test"
531  [3]=>
532  string(27) "PHP: Hypertext Preprocessor"
533  [4]=>
534  string(3) "PHP"
535  [5]=>
536  array(2) {
537    [0]=>
538    string(6) "banana"
539    [1]=>
540    string(6) "orange"
541  }
542  [6]=>
543  int(27)
544  [7]=>
545  float(-0.33333333333333)
546}
547
548 -- Testing sort() --
549No second argument:
550array(8) {
551  [0]=>
552  float(-0.33333333333333)
553  [1]=>
554  string(3) "PHP"
555  [2]=>
556  string(27) "PHP: Hypertext Preprocessor"
557  [3]=>
558  string(4) "Test"
559  [4]=>
560  string(6) "monkey"
561  [5]=>
562  string(4) "test"
563  [6]=>
564  int(27)
565  [7]=>
566  array(2) {
567    [0]=>
568    string(6) "banana"
569    [1]=>
570    string(6) "orange"
571  }
572}
573Using SORT_REGULAR:
574array(8) {
575  [0]=>
576  float(-0.33333333333333)
577  [1]=>
578  string(3) "PHP"
579  [2]=>
580  string(27) "PHP: Hypertext Preprocessor"
581  [3]=>
582  string(4) "Test"
583  [4]=>
584  string(6) "monkey"
585  [5]=>
586  string(4) "test"
587  [6]=>
588  int(27)
589  [7]=>
590  array(2) {
591    [0]=>
592    string(6) "banana"
593    [1]=>
594    string(6) "orange"
595  }
596}
597Using SORT_NUMERIC:
598array(8) {
599  [0]=>
600  float(-0.33333333333333)
601  [1]=>
602  string(6) "monkey"
603  [2]=>
604  string(4) "test"
605  [3]=>
606  string(4) "Test"
607  [4]=>
608  string(27) "PHP: Hypertext Preprocessor"
609  [5]=>
610  string(3) "PHP"
611  [6]=>
612  array(2) {
613    [0]=>
614    string(6) "banana"
615    [1]=>
616    string(6) "orange"
617  }
618  [7]=>
619  int(27)
620}
621Using SORT_STRING
622array(8) {
623  [0]=>
624  float(-0.33333333333333)
625  [1]=>
626  int(27)
627  [2]=>
628  array(2) {
629    [0]=>
630    string(6) "banana"
631    [1]=>
632    string(6) "orange"
633  }
634  [3]=>
635  string(3) "PHP"
636  [4]=>
637  string(27) "PHP: Hypertext Preprocessor"
638  [5]=>
639  string(4) "Test"
640  [6]=>
641  string(6) "monkey"
642  [7]=>
643  string(4) "test"
644}
645