xref: /PHP-7.4/ext/standard/tests/array/002.phpt (revision 26dfce7f)
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.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  [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.33333333333333)
130}
131Using SORT_STRING
132
133Notice: Array to string conversion in %s002.php on line 16
134
135Notice: Array to string conversion in %s002.php on line 16
136
137Notice: Array to string conversion in %s002.php on line 16
138
139Notice: Array to string conversion in %s002.php on line 16
140
141Notice: Array to string conversion in %s002.php on line 16
142
143Notice: Array to string conversion in %s002.php on line 16
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.33333333333333)
166}
167
168 -- Testing asort() --
169No second argument:
170array(8) {
171  [16777216]=>
172  float(-0.33333333333333)
173  [0]=>
174  string(3) "PHP"
175  [17]=>
176  string(27) "PHP: Hypertext Preprocessor"
177  [5]=>
178  string(4) "Test"
179  [1001]=>
180  string(6) "monkey"
181  [1000]=>
182  string(4) "test"
183  ["test"]=>
184  int(27)
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.33333333333333)
197  [0]=>
198  string(3) "PHP"
199  [17]=>
200  string(27) "PHP: Hypertext Preprocessor"
201  [5]=>
202  string(4) "Test"
203  [1001]=>
204  string(6) "monkey"
205  [1000]=>
206  string(4) "test"
207  ["test"]=>
208  int(27)
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.33333333333333)
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
243Notice: Array to string conversion in %s002.php on line 16
244
245Notice: Array to string conversion in %s002.php on line 16
246
247Notice: Array to string conversion in %s002.php on line 16
248
249Notice: Array to string conversion in %s002.php on line 16
250
251Notice: Array to string conversion in %s002.php on line 16
252array(8) {
253  [16777216]=>
254  float(-0.33333333333333)
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  [16777216]=>
280  float(-0.33333333333333)
281  [1001]=>
282  string(6) "monkey"
283  [1000]=>
284  string(4) "test"
285  [17]=>
286  string(27) "PHP: Hypertext Preprocessor"
287  [5]=>
288  string(4) "Test"
289  [0]=>
290  string(3) "PHP"
291  ["test"]=>
292  int(27)
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  [16777216]=>
304  float(-0.33333333333333)
305  [1001]=>
306  string(6) "monkey"
307  [1000]=>
308  string(4) "test"
309  [17]=>
310  string(27) "PHP: Hypertext Preprocessor"
311  [5]=>
312  string(4) "Test"
313  [0]=>
314  string(3) "PHP"
315  ["test"]=>
316  int(27)
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.33333333333333)
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  [0]=>
338  string(3) "PHP"
339  ["test"]=>
340  int(27)
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.33333333333333)
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  ["test"]=>
387  int(27)
388  [5]=>
389  string(4) "Test"
390  [17]=>
391  string(27) "PHP: Hypertext Preprocessor"
392  [1000]=>
393  string(4) "test"
394  [1001]=>
395  string(6) "monkey"
396  [16777216]=>
397  float(-0.33333333333333)
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  ["test"]=>
411  int(27)
412  [5]=>
413  string(4) "Test"
414  [17]=>
415  string(27) "PHP: Hypertext Preprocessor"
416  [1000]=>
417  string(4) "test"
418  [1001]=>
419  string(6) "monkey"
420  [16777216]=>
421  float(-0.33333333333333)
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.33333333333333)
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.33333333333333)
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  int(27)
484  [2]=>
485  string(4) "test"
486  [3]=>
487  string(6) "monkey"
488  [4]=>
489  string(4) "Test"
490  [5]=>
491  string(27) "PHP: Hypertext Preprocessor"
492  [6]=>
493  string(3) "PHP"
494  [7]=>
495  float(-0.33333333333333)
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  int(27)
508  [2]=>
509  string(4) "test"
510  [3]=>
511  string(6) "monkey"
512  [4]=>
513  string(4) "Test"
514  [5]=>
515  string(27) "PHP: Hypertext Preprocessor"
516  [6]=>
517  string(3) "PHP"
518  [7]=>
519  float(-0.33333333333333)
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.33333333333333)
544}
545Using SORT_STRING
546
547Notice: Array to string conversion in %s002.php on line 16
548
549Notice: Array to string conversion in %s002.php on line 16
550
551Notice: Array to string conversion in %s002.php on line 16
552
553Notice: Array to string conversion in %s002.php on line 16
554
555Notice: Array to string conversion in %s002.php on line 16
556
557Notice: Array to string conversion in %s002.php on line 16
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.33333333333333)
580}
581
582 -- Testing sort() --
583No second argument:
584array(8) {
585  [0]=>
586  float(-0.33333333333333)
587  [1]=>
588  string(3) "PHP"
589  [2]=>
590  string(27) "PHP: Hypertext Preprocessor"
591  [3]=>
592  string(4) "Test"
593  [4]=>
594  string(6) "monkey"
595  [5]=>
596  string(4) "test"
597  [6]=>
598  int(27)
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.33333333333333)
611  [1]=>
612  string(3) "PHP"
613  [2]=>
614  string(27) "PHP: Hypertext Preprocessor"
615  [3]=>
616  string(4) "Test"
617  [4]=>
618  string(6) "monkey"
619  [5]=>
620  string(4) "test"
621  [6]=>
622  int(27)
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.33333333333333)
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
657Notice: Array to string conversion in %s002.php on line 16
658
659Notice: Array to string conversion in %s002.php on line 16
660
661Notice: Array to string conversion in %s002.php on line 16
662
663Notice: Array to string conversion in %s002.php on line 16
664
665Notice: Array to string conversion in %s002.php on line 16
666array(8) {
667  [0]=>
668  float(-0.33333333333333)
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