array_map_variation5.phpt 3.4 KB

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