1--TEST--
2Test array_key_exists() function
3--FILE--
4<?php
5/* Prototype:
6 *  bool array_key_exists ( mixed $key, array $search );
7 * Description:
8 *  Returns TRUE if the given key is set in the array.
9 *  key can be any value possible for an array index.
10 *  Also also works on objects.
11 */
12
13echo "*** Testing basic functionalities ***\n";
14/* Arrays with regular values */
15$search_arrays = array(
16           array(1,2,3,4),
17           array('a','b','c'),
18           array('abc', 'bcd', 'dcf'),
19           array("test", "rest", "enjoy"),
20           array("Name" => "Jack", "Loc" => "Mars", "Id" => "MS123"),
21           array('Red' => 'Rose', 'I' => 'You'),
22           array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => "Three" ),
23           array(0.1 => 'Zero', 1.1 => 'One', 2.2 => 'Two', 3.3 => "Three" )
24          );
25/* keys to search in $search_arrays. $keys[0]
26   is the key to be searched in $search_arrays[0] and so on */
27$keys = array( 1, 'a', 2, 4, "Name", "Red", 0, 3 );
28
29$key_counter = 0;
30foreach ($search_arrays as $search_array) {
31  $key = $keys[ $key_counter++ ];
32  echo "-- Iteration $key_counter --\n";
33  var_dump( array_key_exists($key, $search_array) );
34}
35
36echo "\n*** Testing possible variations ***\n";
37// use different keys on each sub array of  $search_arrays
38$key_variations = array ("", NULL, null, " ", '', "test", 1);
39$key_counter = 0;
40$key_count = count ( $key_variations );
41echo "\n** Variation loop 1 **\n";
42$out_loop_count = 0;
43foreach ($search_arrays as $search_array) {
44  $key_counter = 0;
45  $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
46  while ( $key_counter < $key_count ) {
47    $key = $key_variations[ $key_counter++ ];
48    var_dump( array_key_exists($key, $search_array) );
49  }
50}
51// arrays with variation in elements
52$search_arrays_v = array (
53                     array(),
54                     array(NULL),
55                     array(array(), 1, 2),
56                     array(1,2,3, "" => "value", NULL => "value", true => "value" ),
57                     array( array(2,4,5), array ("a","b","d") )
58                   );
59// search for $key_variations in each sub array of $search_arrays_v
60echo "\n** Variation loop 2 **\n";
61$out_loop_count = 0;
62foreach ($search_arrays_v as $search_array) {
63  $key_counter = 0;
64  $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
65  while ( $key_counter < $key_count ) {
66    $key = $key_variations[ $key_counter++ ];
67    var_dump( array_key_exists($key, $search_array) );
68  }
69}
70
71echo "\n*** Testing error conditions ***\n";
72//Zeor args
73var_dump( array_key_exists() );
74// first args as array
75var_dump( array_key_exists(array(), array()) );
76// second args as string
77var_dump( array_key_exists("", "") );
78// second args a integer
79var_dump( array_key_exists(1, 1) );
80// second args as NULL
81var_dump( array_key_exists(1, NULL) );
82// second args as boolean
83var_dump( array_key_exists(1, true) );
84// first args as boolean
85var_dump( array_key_exists(false, true) );
86// second args as float
87var_dump( array_key_exists(false, 17.5) );
88// args more than expected
89var_dump( array_key_exists(1, array(), array()) );
90// first argument as floating point value
91var_dump( array_key_exists(17.5, array(1,23) ) ) ;
92
93echo "\n*** Testing operation on objects ***\n";
94class key_check
95{
96  private   $private_var = "Priviate var";
97  protected $protected_var = "Protected var";
98  public    $public_var = "Public var";
99  public    $arr = array("var" => "value", "1" => "one", ""=>"value");
100  public function print_member()
101  {
102    echo $this->$private_var."\n";
103    echo $this->$protected_var."\n";
104    echo $this->$public_var."\n";
105  }
106}
107
108$key_check_obj = new key_check; //new object
109/* array_key_exists() on an object, it should work on only public member variables */
110var_dump(array_key_exists("private_var", $key_check_obj)); // not found, private member
111var_dump(array_key_exists("protected_var", $key_check_obj)); // not found, private member
112var_dump(array_key_exists("public_var", $key_check_obj)); // found, public member
113var_dump(array_key_exists("print_member", $key_check_obj)); // not found, its a function
114var_dump(array_key_exists("arr", $key_check_obj)); //found, public member
115var_dump(array_key_exists("var", $key_check_obj->arr)); //found,  key is in member array
116
117/* error condition, first argument as object */
118var_dump( array_key_exists($key_check_obj, $key_check_obj) );
119echo "Done\n";
120?>
121--EXPECTF--
122*** Testing basic functionalities ***
123-- Iteration 1 --
124bool(true)
125-- Iteration 2 --
126bool(false)
127-- Iteration 3 --
128bool(true)
129-- Iteration 4 --
130bool(false)
131-- Iteration 5 --
132bool(true)
133-- Iteration 6 --
134bool(true)
135-- Iteration 7 --
136bool(true)
137-- Iteration 8 --
138bool(true)
139
140*** Testing possible variations ***
141
142** Variation loop 1 **
143-- Iteration 1 --
144bool(false)
145bool(false)
146bool(false)
147bool(false)
148bool(false)
149bool(false)
150bool(true)
151-- Iteration 2 --
152bool(false)
153bool(false)
154bool(false)
155bool(false)
156bool(false)
157bool(false)
158bool(true)
159-- Iteration 3 --
160bool(false)
161bool(false)
162bool(false)
163bool(false)
164bool(false)
165bool(false)
166bool(true)
167-- Iteration 4 --
168bool(false)
169bool(false)
170bool(false)
171bool(false)
172bool(false)
173bool(false)
174bool(true)
175-- Iteration 5 --
176bool(false)
177bool(false)
178bool(false)
179bool(false)
180bool(false)
181bool(false)
182bool(false)
183-- Iteration 6 --
184bool(false)
185bool(false)
186bool(false)
187bool(false)
188bool(false)
189bool(false)
190bool(false)
191-- Iteration 7 --
192bool(false)
193bool(false)
194bool(false)
195bool(false)
196bool(false)
197bool(false)
198bool(true)
199-- Iteration 8 --
200bool(false)
201bool(false)
202bool(false)
203bool(false)
204bool(false)
205bool(false)
206bool(true)
207
208** Variation loop 2 **
209-- Iteration 1 --
210bool(false)
211bool(false)
212bool(false)
213bool(false)
214bool(false)
215bool(false)
216bool(false)
217-- Iteration 2 --
218bool(false)
219bool(false)
220bool(false)
221bool(false)
222bool(false)
223bool(false)
224bool(false)
225-- Iteration 3 --
226bool(false)
227bool(false)
228bool(false)
229bool(false)
230bool(false)
231bool(false)
232bool(true)
233-- Iteration 4 --
234bool(true)
235bool(true)
236bool(true)
237bool(false)
238bool(true)
239bool(false)
240bool(true)
241-- Iteration 5 --
242bool(false)
243bool(false)
244bool(false)
245bool(false)
246bool(false)
247bool(false)
248bool(true)
249
250*** Testing error conditions ***
251
252Warning: array_key_exists() expects exactly 2 parameters, 0 given in %s on line %d
253NULL
254
255Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
256bool(false)
257
258Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
259NULL
260
261Warning: array_key_exists() expects parameter 2 to be array, int given in %s on line %d
262NULL
263
264Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
265NULL
266
267Warning: array_key_exists() expects parameter 2 to be array, bool given in %s on line %d
268NULL
269
270Warning: array_key_exists() expects parameter 2 to be array, bool given in %s on line %d
271NULL
272
273Warning: array_key_exists() expects parameter 2 to be array, float given in %s on line %d
274NULL
275
276Warning: array_key_exists() expects exactly 2 parameters, 3 given in %s on line %d
277NULL
278
279Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
280bool(false)
281
282*** Testing operation on objects ***
283bool(false)
284bool(false)
285bool(true)
286bool(false)
287bool(true)
288bool(true)
289
290Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
291bool(false)
292Done
293