1--TEST--
2Test ksort() function : object functionality - sort objects
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 * testing ksort() by providing array ofinteger/string objects with following flag values:
11 *  1.SORT_NUMERIC - compare items numerically
12 *  2.SORT_STRING - compare items as strings
13*/
14
15echo "*** Testing ksort() : object functionality ***\n";
16
17// class declaration for integer objects
18class Integer
19{
20  public $class_value;
21  // initializing object member value
22  function __construct($value){
23    $this->class_value = $value;
24  }
25
26}
27
28// class declaration for string objects
29class String
30{
31  public $class_value;
32  // initializing object member value
33  function __construct($value){
34    $this->class_value = $value;
35  }
36
37  // return string value
38  function __tostring() {
39   return (string)$this->value;
40  }
41
42}
43
44// array of integer objects
45$unsorted_int_obj = array (
46  11 => new Integer(11), 66 =>  new Integer(66),
47  23 => new Integer(23), -5 => new Integer(-5),
48  1 => new Integer(0.001), 0 => new Integer(0)
49);
50
51// array of string objects
52$unsorted_str_obj = array (
53  "axx" => new String("axx"), "t" => new String("t"),
54  "w" => new String("w"), "py" => new String("py"),
55  "apple" => new String("apple"), "Orange" => new String("Orange"),
56  "Lemon" => new String("Lemon"), "aPPle" => new String("aPPle")
57);
58echo "\n-- Testing ksort() by supplying various object arrays, 'flag' value is defualt --\n";
59
60// testing ksort() function by supplying integer object array, flag value is defualt
61$temp_array = $unsorted_int_obj;
62var_dump(ksort($temp_array) );
63var_dump($temp_array);
64
65// testing ksort() function by supplying string object array, flag value is defualt
66$temp_array = $unsorted_str_obj;
67var_dump(ksort($temp_array) );
68var_dump($temp_array);
69
70echo "\n-- Testing ksort() by supplying various object arrays, 'flag' value is SORT_REGULAR --\n";
71// testing ksort() function by supplying integer object array, flag value = SORT_REGULAR
72$temp_array = $unsorted_int_obj;
73var_dump(ksort($temp_array, SORT_REGULAR) );
74var_dump($temp_array);
75
76// testing ksort() function by supplying string object array, flag value = SORT_REGULAR
77$temp_array = $unsorted_str_obj;
78var_dump(ksort($temp_array, SORT_REGULAR) );
79var_dump($temp_array);
80
81echo "Done\n";
82?>
83--EXPECTF--
84*** Testing ksort() : object functionality ***
85
86-- Testing ksort() by supplying various object arrays, 'flag' value is defualt --
87bool(true)
88array(6) {
89  [-5]=>
90  object(Integer)#%d (1) {
91    ["class_value"]=>
92    int(-5)
93  }
94  [0]=>
95  object(Integer)#%d (1) {
96    ["class_value"]=>
97    int(0)
98  }
99  [1]=>
100  object(Integer)#%d (1) {
101    ["class_value"]=>
102    float(0.001)
103  }
104  [11]=>
105  object(Integer)#%d (1) {
106    ["class_value"]=>
107    int(11)
108  }
109  [23]=>
110  object(Integer)#%d (1) {
111    ["class_value"]=>
112    int(23)
113  }
114  [66]=>
115  object(Integer)#%d (1) {
116    ["class_value"]=>
117    int(66)
118  }
119}
120bool(true)
121array(8) {
122  ["Lemon"]=>
123  object(String)#%d (1) {
124    ["class_value"]=>
125    string(5) "Lemon"
126  }
127  ["Orange"]=>
128  object(String)#%d (1) {
129    ["class_value"]=>
130    string(6) "Orange"
131  }
132  ["aPPle"]=>
133  object(String)#%d (1) {
134    ["class_value"]=>
135    string(5) "aPPle"
136  }
137  ["apple"]=>
138  object(String)#%d (1) {
139    ["class_value"]=>
140    string(5) "apple"
141  }
142  ["axx"]=>
143  object(String)#%d (1) {
144    ["class_value"]=>
145    string(3) "axx"
146  }
147  ["py"]=>
148  object(String)#%d (1) {
149    ["class_value"]=>
150    string(2) "py"
151  }
152  ["t"]=>
153  object(String)#%d (1) {
154    ["class_value"]=>
155    string(1) "t"
156  }
157  ["w"]=>
158  object(String)#%d (1) {
159    ["class_value"]=>
160    string(1) "w"
161  }
162}
163
164-- Testing ksort() by supplying various object arrays, 'flag' value is SORT_REGULAR --
165bool(true)
166array(6) {
167  [-5]=>
168  object(Integer)#%d (1) {
169    ["class_value"]=>
170    int(-5)
171  }
172  [0]=>
173  object(Integer)#%d (1) {
174    ["class_value"]=>
175    int(0)
176  }
177  [1]=>
178  object(Integer)#%d (1) {
179    ["class_value"]=>
180    float(0.001)
181  }
182  [11]=>
183  object(Integer)#%d (1) {
184    ["class_value"]=>
185    int(11)
186  }
187  [23]=>
188  object(Integer)#%d (1) {
189    ["class_value"]=>
190    int(23)
191  }
192  [66]=>
193  object(Integer)#%d (1) {
194    ["class_value"]=>
195    int(66)
196  }
197}
198bool(true)
199array(8) {
200  ["Lemon"]=>
201  object(String)#%d (1) {
202    ["class_value"]=>
203    string(5) "Lemon"
204  }
205  ["Orange"]=>
206  object(String)#%d (1) {
207    ["class_value"]=>
208    string(6) "Orange"
209  }
210  ["aPPle"]=>
211  object(String)#%d (1) {
212    ["class_value"]=>
213    string(5) "aPPle"
214  }
215  ["apple"]=>
216  object(String)#%d (1) {
217    ["class_value"]=>
218    string(5) "apple"
219  }
220  ["axx"]=>
221  object(String)#%d (1) {
222    ["class_value"]=>
223    string(3) "axx"
224  }
225  ["py"]=>
226  object(String)#%d (1) {
227    ["class_value"]=>
228    string(2) "py"
229  }
230  ["t"]=>
231  object(String)#%d (1) {
232    ["class_value"]=>
233    string(1) "t"
234  }
235  ["w"]=>
236  object(String)#%d (1) {
237    ["class_value"]=>
238    string(1) "w"
239  }
240}
241Done
242