1--TEST--
2Test ksort() function : basic functionality
3--FILE--
4<?php
5/* Prototype  : bool ksort ( array &$array [, int $sort_flags] )
6 * Description: Sort an array by key, maintaining key to data correlation
7 * Source code: ext/standard/array.c
8*/
9
10/*
11 * Testing ksort() by providing  array of integer/string values to check the basic functionality with following flag values :
12 *  1.flag value as defualt
13 *  2.SORT_REGULAR - compare items normally
14 *  3.SORT_NUMERIC - compare items numerically
15 *  4.SORT_STRING - compare items as strings
16*/
17
18echo "*** Testing ksort() : basic functionality ***\n";
19
20// an array containing unsorted string values with indices
21$unsorted_strings =   array(
22	"l" => "lemon", "o" => "orange",
23	"O" => "Orange", "O1" => "Orange1", "o2" => "orange2", "O3" => "Orange3", "o20" => "orange20",
24	"b" => "banana",
25);
26// an array containing unsorted numeric values with indices
27$unsorted_numerics =  array( 100 => 4, 33 => 3, 555 => 2, 22 => 1 );
28
29echo "\n-- Testing ksort() by supplying string array, 'flag' value is defualt --\n";
30$temp_array = $unsorted_strings;
31var_dump( ksort($temp_array) ); // expecting : bool(true)
32var_dump( $temp_array);
33
34echo "\n-- Testing ksort() by supplying numeric array, 'flag' value is defualt --\n";
35$temp_array = $unsorted_numerics;
36var_dump( ksort($temp_array) ); // expecting : bool(true)
37var_dump( $temp_array);
38
39echo "\n-- Testing ksort() by supplying string array, 'flag' = SORT_REGULAR --\n";
40$temp_array = $unsorted_strings;
41var_dump( ksort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
42var_dump( $temp_array);
43
44echo "\n-- Testing ksort() by supplying numeric array, 'flag' = SORT_REGULAR --\n";
45$temp_array = $unsorted_numerics;
46var_dump( ksort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
47var_dump( $temp_array);
48
49echo "\n-- Testing ksort() by supplying string array, 'flag' = SORT_STRING --\n";
50$temp_array = $unsorted_strings;
51var_dump( ksort($temp_array, SORT_STRING) ); // expecting : bool(true)
52var_dump( $temp_array);
53
54echo "\n-- Testing ksort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --\n";
55$temp_array = $unsorted_strings;
56var_dump( sort($temp_array, SORT_STRING|SORT_FLAG_CASE) ); // expecting : bool(true)
57var_dump( $temp_array);
58
59echo "\n-- Testing ksort() by supplying string array (natural), 'flag' = SORT_NATURAL --\n";
60$temp_array = $unsorted_strings;
61var_dump( sort($temp_array, SORT_NATURAL) ); // expecting : bool(true)
62var_dump( $temp_array);
63
64echo "\n-- Testing ksort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --\n";
65$temp_array = $unsorted_strings;
66var_dump( sort($temp_array, SORT_NATURAL|SORT_FLAG_CASE) ); // expecting : bool(true)
67var_dump( $temp_array);
68
69echo "\n-- Testing ksort() by supplying numeric array, 'flag' = SORT_NUMERIC --\n";
70$temp_array = $unsorted_numerics;
71var_dump( ksort($temp_array, SORT_NUMERIC) ); // expecting : bool(true)
72var_dump( $temp_array);
73
74echo "Done\n";
75?>
76--EXPECTF--
77*** Testing ksort() : basic functionality ***
78
79-- Testing ksort() by supplying string array, 'flag' value is defualt --
80bool(true)
81array(8) {
82  ["O"]=>
83  string(6) "Orange"
84  ["O1"]=>
85  string(7) "Orange1"
86  ["O3"]=>
87  string(7) "Orange3"
88  ["b"]=>
89  string(6) "banana"
90  ["l"]=>
91  string(5) "lemon"
92  ["o"]=>
93  string(6) "orange"
94  ["o2"]=>
95  string(7) "orange2"
96  ["o20"]=>
97  string(8) "orange20"
98}
99
100-- Testing ksort() by supplying numeric array, 'flag' value is defualt --
101bool(true)
102array(4) {
103  [22]=>
104  int(1)
105  [33]=>
106  int(3)
107  [100]=>
108  int(4)
109  [555]=>
110  int(2)
111}
112
113-- Testing ksort() by supplying string array, 'flag' = SORT_REGULAR --
114bool(true)
115array(8) {
116  ["O"]=>
117  string(6) "Orange"
118  ["O1"]=>
119  string(7) "Orange1"
120  ["O3"]=>
121  string(7) "Orange3"
122  ["b"]=>
123  string(6) "banana"
124  ["l"]=>
125  string(5) "lemon"
126  ["o"]=>
127  string(6) "orange"
128  ["o2"]=>
129  string(7) "orange2"
130  ["o20"]=>
131  string(8) "orange20"
132}
133
134-- Testing ksort() by supplying numeric array, 'flag' = SORT_REGULAR --
135bool(true)
136array(4) {
137  [22]=>
138  int(1)
139  [33]=>
140  int(3)
141  [100]=>
142  int(4)
143  [555]=>
144  int(2)
145}
146
147-- Testing ksort() by supplying string array, 'flag' = SORT_STRING --
148bool(true)
149array(8) {
150  ["O"]=>
151  string(6) "Orange"
152  ["O1"]=>
153  string(7) "Orange1"
154  ["O3"]=>
155  string(7) "Orange3"
156  ["b"]=>
157  string(6) "banana"
158  ["l"]=>
159  string(5) "lemon"
160  ["o"]=>
161  string(6) "orange"
162  ["o2"]=>
163  string(7) "orange2"
164  ["o20"]=>
165  string(8) "orange20"
166}
167
168-- Testing ksort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --
169bool(true)
170array(8) {
171  [0]=>
172  string(6) "banana"
173  [1]=>
174  string(5) "lemon"
175  [2]=>
176  string(6) "orange"
177  [3]=>
178  string(6) "Orange"
179  [4]=>
180  string(7) "Orange1"
181  [5]=>
182  string(7) "orange2"
183  [6]=>
184  string(8) "orange20"
185  [7]=>
186  string(7) "Orange3"
187}
188
189-- Testing ksort() by supplying string array (natural), 'flag' = SORT_NATURAL --
190bool(true)
191array(8) {
192  [0]=>
193  string(6) "Orange"
194  [1]=>
195  string(7) "Orange1"
196  [2]=>
197  string(7) "Orange3"
198  [3]=>
199  string(6) "banana"
200  [4]=>
201  string(5) "lemon"
202  [5]=>
203  string(6) "orange"
204  [6]=>
205  string(7) "orange2"
206  [7]=>
207  string(8) "orange20"
208}
209
210-- Testing ksort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --
211bool(true)
212array(8) {
213  [0]=>
214  string(6) "banana"
215  [1]=>
216  string(5) "lemon"
217  [2]=>
218  string(6) "orange"
219  [3]=>
220  string(6) "Orange"
221  [4]=>
222  string(7) "Orange1"
223  [5]=>
224  string(7) "orange2"
225  [6]=>
226  string(7) "Orange3"
227  [7]=>
228  string(8) "orange20"
229}
230
231-- Testing ksort() by supplying numeric array, 'flag' = SORT_NUMERIC --
232bool(true)
233array(4) {
234  [22]=>
235  int(1)
236  [33]=>
237  int(3)
238  [100]=>
239  int(4)
240  [555]=>
241  int(2)
242}
243Done