xref: /PHP-8.4/ext/standard/tests/array/002.phpt (revision 18621521)
1--TEST--
2Test arsort, asort, krsort, ksort, rsort, and sort
3--INI--
4precision=14
5--FILE--
6<?php
7require(__DIR__ . '/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.3333333333333333)
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  [1000]=>
69  string(4) "test"
70  [1001]=>
71  string(6) "monkey"
72  [5]=>
73  string(4) "Test"
74  [17]=>
75  string(27) "PHP: Hypertext Preprocessor"
76  [0]=>
77  string(3) "PHP"
78  ["test"]=>
79  int(27)
80  [16777216]=>
81  float(-0.3333333333333333)
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  [1000]=>
93  string(4) "test"
94  [1001]=>
95  string(6) "monkey"
96  [5]=>
97  string(4) "Test"
98  [17]=>
99  string(27) "PHP: Hypertext Preprocessor"
100  [0]=>
101  string(3) "PHP"
102  ["test"]=>
103  int(27)
104  [16777216]=>
105  float(-0.3333333333333333)
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  [1000]=>
119  string(4) "test"
120  [1001]=>
121  string(6) "monkey"
122  [5]=>
123  string(4) "Test"
124  [17]=>
125  string(27) "PHP: Hypertext Preprocessor"
126  [0]=>
127  string(3) "PHP"
128  [16777216]=>
129  float(-0.3333333333333333)
130}
131Using SORT_STRING
132
133Warning: Array to string conversion in %s on line %d
134
135Warning: Array to string conversion in %s on line %d
136
137Warning: Array to string conversion in %s on line %d
138
139Warning: Array to string conversion in %s on line %d
140
141Warning: Array to string conversion in %s on line %d
142
143Warning: Array to string conversion in %s on line %d
144array(8) {
145  [1000]=>
146  string(4) "test"
147  [1001]=>
148  string(6) "monkey"
149  [5]=>
150  string(4) "Test"
151  [17]=>
152  string(27) "PHP: Hypertext Preprocessor"
153  [0]=>
154  string(3) "PHP"
155  [-1000]=>
156  array(2) {
157    [0]=>
158    string(6) "banana"
159    [1]=>
160    string(6) "orange"
161  }
162  ["test"]=>
163  int(27)
164  [16777216]=>
165  float(-0.3333333333333333)
166}
167
168 -- Testing asort() --
169No second argument:
170array(8) {
171  [16777216]=>
172  float(-0.3333333333333333)
173  ["test"]=>
174  int(27)
175  [0]=>
176  string(3) "PHP"
177  [17]=>
178  string(27) "PHP: Hypertext Preprocessor"
179  [5]=>
180  string(4) "Test"
181  [1001]=>
182  string(6) "monkey"
183  [1000]=>
184  string(4) "test"
185  [-1000]=>
186  array(2) {
187    [0]=>
188    string(6) "banana"
189    [1]=>
190    string(6) "orange"
191  }
192}
193Using SORT_REGULAR:
194array(8) {
195  [16777216]=>
196  float(-0.3333333333333333)
197  ["test"]=>
198  int(27)
199  [0]=>
200  string(3) "PHP"
201  [17]=>
202  string(27) "PHP: Hypertext Preprocessor"
203  [5]=>
204  string(4) "Test"
205  [1001]=>
206  string(6) "monkey"
207  [1000]=>
208  string(4) "test"
209  [-1000]=>
210  array(2) {
211    [0]=>
212    string(6) "banana"
213    [1]=>
214    string(6) "orange"
215  }
216}
217Using SORT_NUMERIC:
218array(8) {
219  [16777216]=>
220  float(-0.3333333333333333)
221  [0]=>
222  string(3) "PHP"
223  [17]=>
224  string(27) "PHP: Hypertext Preprocessor"
225  [5]=>
226  string(4) "Test"
227  [1001]=>
228  string(6) "monkey"
229  [1000]=>
230  string(4) "test"
231  [-1000]=>
232  array(2) {
233    [0]=>
234    string(6) "banana"
235    [1]=>
236    string(6) "orange"
237  }
238  ["test"]=>
239  int(27)
240}
241Using SORT_STRING
242
243Warning: Array to string conversion in %s on line %d
244
245Warning: Array to string conversion in %s on line %d
246
247Warning: Array to string conversion in %s on line %d
248
249Warning: Array to string conversion in %s on line %d
250
251Warning: Array to string conversion in %s on line %d
252array(8) {
253  [16777216]=>
254  float(-0.3333333333333333)
255  ["test"]=>
256  int(27)
257  [-1000]=>
258  array(2) {
259    [0]=>
260    string(6) "banana"
261    [1]=>
262    string(6) "orange"
263  }
264  [0]=>
265  string(3) "PHP"
266  [17]=>
267  string(27) "PHP: Hypertext Preprocessor"
268  [5]=>
269  string(4) "Test"
270  [1001]=>
271  string(6) "monkey"
272  [1000]=>
273  string(4) "test"
274}
275
276 -- Testing krsort() --
277No second argument:
278array(8) {
279  ["test"]=>
280  int(27)
281  [16777216]=>
282  float(-0.3333333333333333)
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  [-1000]=>
294  array(2) {
295    [0]=>
296    string(6) "banana"
297    [1]=>
298    string(6) "orange"
299  }
300}
301Using SORT_REGULAR:
302array(8) {
303  ["test"]=>
304  int(27)
305  [16777216]=>
306  float(-0.3333333333333333)
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  [0]=>
316  string(3) "PHP"
317  [-1000]=>
318  array(2) {
319    [0]=>
320    string(6) "banana"
321    [1]=>
322    string(6) "orange"
323  }
324}
325Using SORT_NUMERIC:
326array(8) {
327  [16777216]=>
328  float(-0.3333333333333333)
329  [1001]=>
330  string(6) "monkey"
331  [1000]=>
332  string(4) "test"
333  [17]=>
334  string(27) "PHP: Hypertext Preprocessor"
335  [5]=>
336  string(4) "Test"
337  ["test"]=>
338  int(27)
339  [0]=>
340  string(3) "PHP"
341  [-1000]=>
342  array(2) {
343    [0]=>
344    string(6) "banana"
345    [1]=>
346    string(6) "orange"
347  }
348}
349Using SORT_STRING
350array(8) {
351  ["test"]=>
352  int(27)
353  [5]=>
354  string(4) "Test"
355  [17]=>
356  string(27) "PHP: Hypertext Preprocessor"
357  [16777216]=>
358  float(-0.3333333333333333)
359  [1001]=>
360  string(6) "monkey"
361  [1000]=>
362  string(4) "test"
363  [0]=>
364  string(3) "PHP"
365  [-1000]=>
366  array(2) {
367    [0]=>
368    string(6) "banana"
369    [1]=>
370    string(6) "orange"
371  }
372}
373
374 -- Testing ksort() --
375No second argument:
376array(8) {
377  [-1000]=>
378  array(2) {
379    [0]=>
380    string(6) "banana"
381    [1]=>
382    string(6) "orange"
383  }
384  [0]=>
385  string(3) "PHP"
386  [5]=>
387  string(4) "Test"
388  [17]=>
389  string(27) "PHP: Hypertext Preprocessor"
390  [1000]=>
391  string(4) "test"
392  [1001]=>
393  string(6) "monkey"
394  [16777216]=>
395  float(-0.3333333333333333)
396  ["test"]=>
397  int(27)
398}
399Using SORT_REGULAR:
400array(8) {
401  [-1000]=>
402  array(2) {
403    [0]=>
404    string(6) "banana"
405    [1]=>
406    string(6) "orange"
407  }
408  [0]=>
409  string(3) "PHP"
410  [5]=>
411  string(4) "Test"
412  [17]=>
413  string(27) "PHP: Hypertext Preprocessor"
414  [1000]=>
415  string(4) "test"
416  [1001]=>
417  string(6) "monkey"
418  [16777216]=>
419  float(-0.3333333333333333)
420  ["test"]=>
421  int(27)
422}
423Using SORT_NUMERIC:
424array(8) {
425  [-1000]=>
426  array(2) {
427    [0]=>
428    string(6) "banana"
429    [1]=>
430    string(6) "orange"
431  }
432  [0]=>
433  string(3) "PHP"
434  ["test"]=>
435  int(27)
436  [5]=>
437  string(4) "Test"
438  [17]=>
439  string(27) "PHP: Hypertext Preprocessor"
440  [1000]=>
441  string(4) "test"
442  [1001]=>
443  string(6) "monkey"
444  [16777216]=>
445  float(-0.3333333333333333)
446}
447Using SORT_STRING
448array(8) {
449  [-1000]=>
450  array(2) {
451    [0]=>
452    string(6) "banana"
453    [1]=>
454    string(6) "orange"
455  }
456  [0]=>
457  string(3) "PHP"
458  [1000]=>
459  string(4) "test"
460  [1001]=>
461  string(6) "monkey"
462  [16777216]=>
463  float(-0.3333333333333333)
464  [17]=>
465  string(27) "PHP: Hypertext Preprocessor"
466  [5]=>
467  string(4) "Test"
468  ["test"]=>
469  int(27)
470}
471
472 -- Testing rsort() --
473No second argument:
474array(8) {
475  [0]=>
476  array(2) {
477    [0]=>
478    string(6) "banana"
479    [1]=>
480    string(6) "orange"
481  }
482  [1]=>
483  string(4) "test"
484  [2]=>
485  string(6) "monkey"
486  [3]=>
487  string(4) "Test"
488  [4]=>
489  string(27) "PHP: Hypertext Preprocessor"
490  [5]=>
491  string(3) "PHP"
492  [6]=>
493  int(27)
494  [7]=>
495  float(-0.3333333333333333)
496}
497Using SORT_REGULAR:
498array(8) {
499  [0]=>
500  array(2) {
501    [0]=>
502    string(6) "banana"
503    [1]=>
504    string(6) "orange"
505  }
506  [1]=>
507  string(4) "test"
508  [2]=>
509  string(6) "monkey"
510  [3]=>
511  string(4) "Test"
512  [4]=>
513  string(27) "PHP: Hypertext Preprocessor"
514  [5]=>
515  string(3) "PHP"
516  [6]=>
517  int(27)
518  [7]=>
519  float(-0.3333333333333333)
520}
521Using SORT_NUMERIC:
522array(8) {
523  [0]=>
524  int(27)
525  [1]=>
526  array(2) {
527    [0]=>
528    string(6) "banana"
529    [1]=>
530    string(6) "orange"
531  }
532  [2]=>
533  string(4) "test"
534  [3]=>
535  string(6) "monkey"
536  [4]=>
537  string(4) "Test"
538  [5]=>
539  string(27) "PHP: Hypertext Preprocessor"
540  [6]=>
541  string(3) "PHP"
542  [7]=>
543  float(-0.3333333333333333)
544}
545Using SORT_STRING
546
547Warning: Array to string conversion in %s on line %d
548
549Warning: Array to string conversion in %s on line %d
550
551Warning: Array to string conversion in %s on line %d
552
553Warning: Array to string conversion in %s on line %d
554
555Warning: Array to string conversion in %s on line %d
556
557Warning: Array to string conversion in %s on line %d
558array(8) {
559  [0]=>
560  string(4) "test"
561  [1]=>
562  string(6) "monkey"
563  [2]=>
564  string(4) "Test"
565  [3]=>
566  string(27) "PHP: Hypertext Preprocessor"
567  [4]=>
568  string(3) "PHP"
569  [5]=>
570  array(2) {
571    [0]=>
572    string(6) "banana"
573    [1]=>
574    string(6) "orange"
575  }
576  [6]=>
577  int(27)
578  [7]=>
579  float(-0.3333333333333333)
580}
581
582 -- Testing sort() --
583No second argument:
584array(8) {
585  [0]=>
586  float(-0.3333333333333333)
587  [1]=>
588  int(27)
589  [2]=>
590  string(3) "PHP"
591  [3]=>
592  string(27) "PHP: Hypertext Preprocessor"
593  [4]=>
594  string(4) "Test"
595  [5]=>
596  string(6) "monkey"
597  [6]=>
598  string(4) "test"
599  [7]=>
600  array(2) {
601    [0]=>
602    string(6) "banana"
603    [1]=>
604    string(6) "orange"
605  }
606}
607Using SORT_REGULAR:
608array(8) {
609  [0]=>
610  float(-0.3333333333333333)
611  [1]=>
612  int(27)
613  [2]=>
614  string(3) "PHP"
615  [3]=>
616  string(27) "PHP: Hypertext Preprocessor"
617  [4]=>
618  string(4) "Test"
619  [5]=>
620  string(6) "monkey"
621  [6]=>
622  string(4) "test"
623  [7]=>
624  array(2) {
625    [0]=>
626    string(6) "banana"
627    [1]=>
628    string(6) "orange"
629  }
630}
631Using SORT_NUMERIC:
632array(8) {
633  [0]=>
634  float(-0.3333333333333333)
635  [1]=>
636  string(3) "PHP"
637  [2]=>
638  string(27) "PHP: Hypertext Preprocessor"
639  [3]=>
640  string(4) "Test"
641  [4]=>
642  string(6) "monkey"
643  [5]=>
644  string(4) "test"
645  [6]=>
646  array(2) {
647    [0]=>
648    string(6) "banana"
649    [1]=>
650    string(6) "orange"
651  }
652  [7]=>
653  int(27)
654}
655Using SORT_STRING
656
657Warning: Array to string conversion in %s on line %d
658
659Warning: Array to string conversion in %s on line %d
660
661Warning: Array to string conversion in %s on line %d
662
663Warning: Array to string conversion in %s on line %d
664
665Warning: Array to string conversion in %s on line %d
666array(8) {
667  [0]=>
668  float(-0.3333333333333333)
669  [1]=>
670  int(27)
671  [2]=>
672  array(2) {
673    [0]=>
674    string(6) "banana"
675    [1]=>
676    string(6) "orange"
677  }
678  [3]=>
679  string(3) "PHP"
680  [4]=>
681  string(27) "PHP: Hypertext Preprocessor"
682  [5]=>
683  string(4) "Test"
684  [6]=>
685  string(6) "monkey"
686  [7]=>
687  string(4) "test"
688}
689