array_combine_variation5.phpt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. --TEST--
  2. Test array_combine() function : usage variations - associative array with different values(Bug#43424)
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_combine(array $keys, array $values)
  6. * Description: Creates an array by using the elements of the first parameter as keys
  7. * and the elements of the second as the corresponding values
  8. * Source code: ext/standard/array.c
  9. */
  10. /*
  11. * Testing the functionality of array_combine() by passing various
  12. * associative arrays having different possible values to $keys argument and
  13. * associative arrays having different possible values to $values argument.
  14. */
  15. echo "*** Testing array_combine() : assoc array with diff values to both \$keys and \$values argument ***\n";
  16. // get an unset variable
  17. $unset_var = 10;
  18. unset ($unset_var);
  19. // get a resource variable
  20. $fp = fopen(__FILE__, "r");
  21. // get a class
  22. class classA
  23. {
  24. public function __toString(){
  25. return "Class A object";
  26. }
  27. }
  28. // get a heredoc string
  29. $heredoc = <<<EOT
  30. Hello world
  31. EOT;
  32. // different variations of associative array
  33. $arrays = array (
  34. // empty array
  35. /*1*/ array(),
  36. // arrays with integer values
  37. array('0' => 0),
  38. array("1" => 1),
  39. array("one" => 1, 'two' => 2, "three" => 3, 4 => 4),
  40. // arrays with float values
  41. /*5*/ array("float" => 2.3333),
  42. array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 33333333.333),
  43. // arrays with string values
  44. /*7*/ array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "pen\n"),
  45. array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => 'pen\n'),
  46. array(1 => "hello", "heredoc" => $heredoc),
  47. // array with object, unset variable and resource variable
  48. /*10*/ array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp),
  49. // array with mixed values
  50. /*11*/ array(1 => 'hello', 2 => new classA(), 222 => "fruit",
  51. 'resource' => $fp, "int" => 133, "float" => 444.432,
  52. "unset" => @$unset_var, "heredoc" => $heredoc)
  53. );
  54. // loop through each sub-array within $arrays to check the behavior of array_combine()
  55. $iterator = 1;
  56. foreach($arrays as $array) {
  57. echo "-- Iteration $iterator --\n";
  58. var_dump( array_combine($array, $array) );
  59. $iterator++;
  60. }
  61. // close the file resource used
  62. fclose($fp);
  63. echo "Done";
  64. ?>
  65. --EXPECTF--
  66. *** Testing array_combine() : assoc array with diff values to both $keys and $values argument ***
  67. -- Iteration 1 --
  68. array(0) {
  69. }
  70. -- Iteration 2 --
  71. array(1) {
  72. [0]=>
  73. int(0)
  74. }
  75. -- Iteration 3 --
  76. array(1) {
  77. [1]=>
  78. int(1)
  79. }
  80. -- Iteration 4 --
  81. array(4) {
  82. [1]=>
  83. int(1)
  84. [2]=>
  85. int(2)
  86. [3]=>
  87. int(3)
  88. [4]=>
  89. int(4)
  90. }
  91. -- Iteration 5 --
  92. array(1) {
  93. ["2.3333"]=>
  94. float(2.3333)
  95. }
  96. -- Iteration 6 --
  97. array(4) {
  98. ["1.2"]=>
  99. float(1.2)
  100. ["3.33"]=>
  101. float(3.33)
  102. ["4.8999992284"]=>
  103. float(4.8999992284)
  104. ["33333333.333"]=>
  105. float(33333333.333)
  106. }
  107. -- Iteration 7 --
  108. array(4) {
  109. [" Hello"]=>
  110. string(6) " Hello"
  111. ["col or"]=>
  112. string(6) "col or"
  113. [" world"]=>
  114. string(7) " world"
  115. ["pen
  116. "]=>
  117. string(4) "pen
  118. "
  119. }
  120. -- Iteration 8 --
  121. array(4) {
  122. ["\tHello"]=>
  123. string(7) "\tHello"
  124. ["col\tor"]=>
  125. string(7) "col\tor"
  126. ["\v\fworld"]=>
  127. string(9) "\v\fworld"
  128. ["pen\n"]=>
  129. string(5) "pen\n"
  130. }
  131. -- Iteration 9 --
  132. array(2) {
  133. ["hello"]=>
  134. string(5) "hello"
  135. ["Hello world"]=>
  136. string(11) "Hello world"
  137. }
  138. -- Iteration 10 --
  139. array(3) {
  140. ["Class A object"]=>
  141. object(classA)#%d (0) {
  142. }
  143. [""]=>
  144. NULL
  145. ["Resource id #%d"]=>
  146. resource(%d) of type (stream)
  147. }
  148. -- Iteration 11 --
  149. array(8) {
  150. ["hello"]=>
  151. string(5) "hello"
  152. ["Class A object"]=>
  153. object(classA)#%d (0) {
  154. }
  155. ["fruit"]=>
  156. string(5) "fruit"
  157. ["Resource id #%d"]=>
  158. resource(%d) of type (stream)
  159. [133]=>
  160. int(133)
  161. ["444.432"]=>
  162. float(444.432)
  163. [""]=>
  164. NULL
  165. ["Hello world"]=>
  166. string(11) "Hello world"
  167. }
  168. Done