array_values_variation2.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. --TEST--
  2. Test array_values() function : usage variations - arrays of different data types
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_values(array $input)
  6. * Description: Return just the values from the input array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Pass arrays of different data types as $input argument to array_values() to test behaviour
  11. */
  12. echo "*** Testing array_values() : usage variations ***\n";
  13. //get an unset variable
  14. $unset_var = 10;
  15. unset ($unset_var);
  16. // get a class
  17. class classA
  18. {
  19. public function __toString() {
  20. return "Class A object";
  21. }
  22. }
  23. // heredoc string
  24. $heredoc = <<<EOT
  25. hello world
  26. EOT;
  27. // get a resource variable
  28. $fp = fopen(__FILE__, "r");
  29. // arrays of different data types to be passed as $input
  30. $inputs = array(
  31. // int data
  32. /*1*/ 'int' => array(
  33. 0,
  34. 1,
  35. 12345,
  36. -2345,
  37. ),
  38. // float data
  39. /*2*/ 'float' => array(
  40. 10.5,
  41. -10.5,
  42. 12.3456789000e10,
  43. 12.3456789000E-10,
  44. .5,
  45. ),
  46. // null data
  47. /*3*/ 'null' => array(
  48. NULL,
  49. null,
  50. ),
  51. // boolean data
  52. /*4*/ 'bool' => array(
  53. true,
  54. false,
  55. TRUE,
  56. FALSE,
  57. ),
  58. // empty data
  59. /*5*/ 'empty string' => array(
  60. "",
  61. '',
  62. ),
  63. /*6*/ 'empty array' => array(
  64. ),
  65. // string data
  66. /*7*/ 'string' => array(
  67. "string",
  68. 'string',
  69. $heredoc,
  70. ),
  71. // object data
  72. /*8*/ 'object' => array(
  73. new classA(),
  74. ),
  75. // undefined data
  76. /*9*/ 'undefined' => array(
  77. @$undefined_var,
  78. ),
  79. // unset data
  80. /*10*/ 'unset' => array(
  81. @$unset_var,
  82. ),
  83. // resource variable
  84. /*11*/ 'resource' => array(
  85. $fp
  86. ),
  87. );
  88. // loop through each element of $inputs to check the behavior of array_values()
  89. $iterator = 1;
  90. foreach($inputs as $key => $input) {
  91. echo "\n-- Iteration $iterator: $key data --\n";
  92. var_dump( array_values($input) );
  93. $iterator++;
  94. };
  95. fclose($fp);
  96. echo "Done";
  97. ?>
  98. --EXPECTF--
  99. *** Testing array_values() : usage variations ***
  100. -- Iteration 1: int data --
  101. array(4) {
  102. [0]=>
  103. int(0)
  104. [1]=>
  105. int(1)
  106. [2]=>
  107. int(12345)
  108. [3]=>
  109. int(-2345)
  110. }
  111. -- Iteration 2: float data --
  112. array(5) {
  113. [0]=>
  114. float(10.5)
  115. [1]=>
  116. float(-10.5)
  117. [2]=>
  118. float(123456789000)
  119. [3]=>
  120. float(1.23456789E-9)
  121. [4]=>
  122. float(0.5)
  123. }
  124. -- Iteration 3: null data --
  125. array(2) {
  126. [0]=>
  127. NULL
  128. [1]=>
  129. NULL
  130. }
  131. -- Iteration 4: bool data --
  132. array(4) {
  133. [0]=>
  134. bool(true)
  135. [1]=>
  136. bool(false)
  137. [2]=>
  138. bool(true)
  139. [3]=>
  140. bool(false)
  141. }
  142. -- Iteration 5: empty string data --
  143. array(2) {
  144. [0]=>
  145. string(0) ""
  146. [1]=>
  147. string(0) ""
  148. }
  149. -- Iteration 6: empty array data --
  150. array(0) {
  151. }
  152. -- Iteration 7: string data --
  153. array(3) {
  154. [0]=>
  155. string(6) "string"
  156. [1]=>
  157. string(6) "string"
  158. [2]=>
  159. string(11) "hello world"
  160. }
  161. -- Iteration 8: object data --
  162. array(1) {
  163. [0]=>
  164. object(classA)#%d (0) {
  165. }
  166. }
  167. -- Iteration 9: undefined data --
  168. array(1) {
  169. [0]=>
  170. NULL
  171. }
  172. -- Iteration 10: unset data --
  173. array(1) {
  174. [0]=>
  175. NULL
  176. }
  177. -- Iteration 11: resource data --
  178. array(1) {
  179. [0]=>
  180. resource(%d) of type (stream)
  181. }
  182. Done