summaryrefslogtreecommitdiff
path: root/ext/standard/tests/array/array_key_exists.phpt
blob: 3e0e5ffaaaac9c09b8d0240dc605a59cba5f9108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
--TEST--
Test array_key_exists() function
--FILE--
<?php
echo "*** Testing basic functionalities ***\n";
/* Arrays with regular values */
$search_arrays = array(
           array(1,2,3,4),
           array('a','b','c'),
           array('abc', 'bcd', 'dcf'),
           array("test", "rest", "enjoy"),
           array("Name" => "Jack", "Loc" => "Mars", "Id" => "MS123"),
           array('Red' => 'Rose', 'I' => 'You'),
           array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => "Three" ),
           array(0.1 => 'Zero', 1.1 => 'One', 2.2 => 'Two', 3.3 => "Three" )
          );
/* keys to search in $search_arrays. $keys[0]
   is the key to be searched in $search_arrays[0] and so on */
$keys = array( 1, 'a', 2, 4, "Name", "Red", 0, 3 );

$key_counter = 0;
foreach ($search_arrays as $search_array) {
  $key = $keys[ $key_counter++ ];
  echo "-- Iteration $key_counter --\n";
  var_dump( array_key_exists($key, $search_array) );
}

echo "\n*** Testing possible variations ***\n";
// use different keys on each sub array of  $search_arrays
$key_variations = array ("", NULL, null, " ", '', "test", 1);
$key_counter = 0;
$key_count = count ( $key_variations );
echo "\n** Variation loop 1 **\n";
$out_loop_count = 0;
foreach ($search_arrays as $search_array) {
  $key_counter = 0;
  $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
  while ( $key_counter < $key_count ) {
    $key = $key_variations[ $key_counter++ ];
    var_dump( array_key_exists($key, $search_array) );
  }
}
// arrays with variation in elements
$search_arrays_v = array (
                     array(),
                     array(NULL),
                     array(array(), 1, 2),
                     array(1,2,3, "" => "value", NULL => "value", true => "value" ),
                     array( array(2,4,5), array ("a","b","d") )
                   );
// search for $key_variations in each sub array of $search_arrays_v
echo "\n** Variation loop 2 **\n";
$out_loop_count = 0;
foreach ($search_arrays_v as $search_array) {
  $key_counter = 0;
  $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
  while ( $key_counter < $key_count ) {
    $key = $key_variations[ $key_counter++ ];
    var_dump( array_key_exists($key, $search_array) );
  }
}

echo "\n*** Testing error conditions ***\n";
// first args as array
try {
    array_key_exists(array(), array());
} catch (TypeError $exception) {
    echo $exception->getMessage() . "\n";
}

echo "\n*** Testing operation on objects ***\n";
class key_check
{
  public $public_var = "Public var";
}

$key_check_obj = new key_check; //new object
try {
    var_dump(array_key_exists("public_var", $key_check_obj));
} catch (TypeError $e) {
    echo $e->getMessage(), "\n";
}

echo "Done\n";
?>
--EXPECT--
*** Testing basic functionalities ***
-- Iteration 1 --
bool(true)
-- Iteration 2 --
bool(false)
-- Iteration 3 --
bool(true)
-- Iteration 4 --
bool(false)
-- Iteration 5 --
bool(true)
-- Iteration 6 --
bool(true)
-- Iteration 7 --
bool(true)
-- Iteration 8 --
bool(true)

*** Testing possible variations ***

** Variation loop 1 **
-- Iteration 1 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
-- Iteration 2 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
-- Iteration 3 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
-- Iteration 4 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
-- Iteration 5 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
-- Iteration 6 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
-- Iteration 7 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
-- Iteration 8 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)

** Variation loop 2 **
-- Iteration 1 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
-- Iteration 2 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
-- Iteration 3 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
-- Iteration 4 --
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
-- Iteration 5 --
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)

*** Testing error conditions ***
Illegal offset type

*** Testing operation on objects ***
array_key_exists(): Argument #2 ($array) must be of type array, key_check given
Done