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