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