1--TEST--
2Test array_key_exists() function
3--FILE--
4<?php
5echo "*** Testing basic functionalities ***\n";
6/* Arrays with regular values */
7$search_arrays = array(
8           array(1,2,3,4),
9           array('a','b','c'),
10           array('abc', 'bcd', 'dcf'),
11           array("test", "rest", "enjoy"),
12           array("Name" => "Jack", "Loc" => "Mars", "Id" => "MS123"),
13           array('Red' => 'Rose', 'I' => 'You'),
14           array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => "Three" ),
15          );
16/* keys to search in $search_arrays. $keys[0]
17   is the key to be searched in $search_arrays[0] and so on */
18$keys = array( 1, 'a', 2, 4, "Name", "Red", 0, 3 );
19
20$key_counter = 0;
21foreach ($search_arrays as $search_array) {
22  $key = $keys[ $key_counter++ ];
23  echo "-- Iteration $key_counter --\n";
24  var_dump( array_key_exists($key, $search_array) );
25}
26
27echo "\n*** Testing possible variations ***\n";
28// use different keys on each sub array of  $search_arrays
29$key_variations = array ("", NULL, null, " ", '', "test", 1);
30$key_counter = 0;
31$key_count = count ( $key_variations );
32echo "\n** Variation loop 1 **\n";
33$out_loop_count = 0;
34foreach ($search_arrays as $search_array) {
35  $key_counter = 0;
36  $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
37  while ( $key_counter < $key_count ) {
38    $key = $key_variations[ $key_counter++ ];
39    var_dump( array_key_exists($key, $search_array) );
40  }
41}
42// arrays with variation in elements
43$search_arrays_v = array (
44                     array(),
45                     array(NULL),
46                     array(array(), 1, 2),
47                     array(1,2,3, "" => "value", NULL => "value", true => "value" ),
48                     array( array(2,4,5), array ("a","b","d") )
49                   );
50// search for $key_variations in each sub array of $search_arrays_v
51echo "\n** Variation loop 2 **\n";
52$out_loop_count = 0;
53foreach ($search_arrays_v as $search_array) {
54  $key_counter = 0;
55  $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
56  while ( $key_counter < $key_count ) {
57    $key = $key_variations[ $key_counter++ ];
58    var_dump( array_key_exists($key, $search_array) );
59  }
60}
61
62echo "\n*** Testing error conditions ***\n";
63// first args as array
64try {
65    array_key_exists(array(), array());
66} catch (TypeError $exception) {
67    echo $exception->getMessage() . "\n";
68}
69
70echo "\n*** Testing operation on objects ***\n";
71class key_check
72{
73  public $public_var = "Public var";
74}
75
76$key_check_obj = new key_check; //new object
77try {
78    var_dump(array_key_exists("public_var", $key_check_obj));
79} catch (TypeError $e) {
80    echo $e->getMessage(), "\n";
81}
82
83echo "Done\n";
84?>
85--EXPECT--
86*** Testing basic functionalities ***
87-- Iteration 1 --
88bool(true)
89-- Iteration 2 --
90bool(false)
91-- Iteration 3 --
92bool(true)
93-- Iteration 4 --
94bool(false)
95-- Iteration 5 --
96bool(true)
97-- Iteration 6 --
98bool(true)
99-- Iteration 7 --
100bool(true)
101
102*** Testing possible variations ***
103
104** Variation loop 1 **
105-- Iteration 1 --
106bool(false)
107bool(false)
108bool(false)
109bool(false)
110bool(false)
111bool(false)
112bool(true)
113-- Iteration 2 --
114bool(false)
115bool(false)
116bool(false)
117bool(false)
118bool(false)
119bool(false)
120bool(true)
121-- Iteration 3 --
122bool(false)
123bool(false)
124bool(false)
125bool(false)
126bool(false)
127bool(false)
128bool(true)
129-- Iteration 4 --
130bool(false)
131bool(false)
132bool(false)
133bool(false)
134bool(false)
135bool(false)
136bool(true)
137-- Iteration 5 --
138bool(false)
139bool(false)
140bool(false)
141bool(false)
142bool(false)
143bool(false)
144bool(false)
145-- Iteration 6 --
146bool(false)
147bool(false)
148bool(false)
149bool(false)
150bool(false)
151bool(false)
152bool(false)
153-- Iteration 7 --
154bool(false)
155bool(false)
156bool(false)
157bool(false)
158bool(false)
159bool(false)
160bool(true)
161
162** Variation loop 2 **
163-- Iteration 1 --
164bool(false)
165bool(false)
166bool(false)
167bool(false)
168bool(false)
169bool(false)
170bool(false)
171-- Iteration 2 --
172bool(false)
173bool(false)
174bool(false)
175bool(false)
176bool(false)
177bool(false)
178bool(false)
179-- Iteration 3 --
180bool(false)
181bool(false)
182bool(false)
183bool(false)
184bool(false)
185bool(false)
186bool(true)
187-- Iteration 4 --
188bool(true)
189bool(true)
190bool(true)
191bool(false)
192bool(true)
193bool(false)
194bool(true)
195-- Iteration 5 --
196bool(false)
197bool(false)
198bool(false)
199bool(false)
200bool(false)
201bool(false)
202bool(true)
203
204*** Testing error conditions ***
205Illegal offset type
206
207*** Testing operation on objects ***
208array_key_exists(): Argument #2 ($array) must be of type array, key_check given
209Done
210