array_search_variation2.phpt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. --TEST--
  2. Test array_search() function : usage variations - different haystack values
  3. --FILE--
  4. <?php
  5. /*
  6. * Prototype : mixed array_search ( mixed $needle, array $haystack [, bool $strict] )
  7. * Description: Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise
  8. * Source Code: ext/standard/array.c
  9. */
  10. /* Test array_search() with different possible haystack values */
  11. echo "*** Testing array_search() with different haystack values ***\n";
  12. $misc_array = array (
  13. 'a',
  14. 'key' =>'d',
  15. 3,
  16. ".001" =>-67,
  17. "-.051" =>"k",
  18. 0.091 =>"-.08",
  19. "e" =>"5",
  20. "y" =>NULL,
  21. NULL =>"",
  22. 0,
  23. TRUE,
  24. FALSE,
  25. -27.39999999999,
  26. " ",
  27. "abcd\x00abcd\x00\abcd\x00abcdefghij",
  28. "abcd\nabcd\tabcd\rabcd\0abcd"
  29. );
  30. $array_type = array(TRUE, FALSE, 1, 0, -1, "1", "0", "-1", NULL, array(), "PHP", "");
  31. /* loop to do loose and strict type check of elements in
  32. $array_type on elements in $misc_array using array_search();
  33. checking PHP type comparison tables
  34. */
  35. $counter = 1;
  36. foreach($array_type as $type) {
  37. echo "-- Iteration $counter --\n";
  38. //loose type checking
  39. var_dump( array_search($type,$misc_array ) );
  40. //strict type checking
  41. var_dump( array_search($type,$misc_array,true) );
  42. //loose type checking
  43. var_dump( array_search($type,$misc_array,false) );
  44. $counter++;
  45. }
  46. echo "Done\n";
  47. ?>
  48. --EXPECTF--
  49. *** Testing array_search() with different haystack values ***
  50. -- Iteration 1 --
  51. int(0)
  52. int(3)
  53. int(0)
  54. -- Iteration 2 --
  55. string(1) "y"
  56. int(4)
  57. string(1) "y"
  58. -- Iteration 3 --
  59. int(3)
  60. bool(false)
  61. int(3)
  62. -- Iteration 4 --
  63. string(3) "key"
  64. int(2)
  65. string(3) "key"
  66. -- Iteration 5 --
  67. int(3)
  68. bool(false)
  69. int(3)
  70. -- Iteration 6 --
  71. int(3)
  72. bool(false)
  73. int(3)
  74. -- Iteration 7 --
  75. int(2)
  76. bool(false)
  77. int(2)
  78. -- Iteration 8 --
  79. int(3)
  80. bool(false)
  81. int(3)
  82. -- Iteration 9 --
  83. string(1) "y"
  84. string(1) "y"
  85. string(1) "y"
  86. -- Iteration 10 --
  87. string(1) "y"
  88. bool(false)
  89. string(1) "y"
  90. -- Iteration 11 --
  91. int(2)
  92. bool(false)
  93. int(2)
  94. -- Iteration 12 --
  95. string(1) "y"
  96. string(0) ""
  97. string(1) "y"
  98. Done