array_key_exists.phpt 7.2 KB

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