array_unshift_variation4.phpt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. --TEST--
  2. Test array_unshift() function : usage variations - assoc. array with diff. keys for 'array' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : int array_unshift(array $array, mixed $var [, mixed ...])
  6. * Description: Pushes elements onto the beginning of the array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Testing the functionality of array_unshift() by passing different
  11. * associative arrays having different possible keys to $array argument.
  12. * The $var argument passed is a fixed value
  13. */
  14. echo "*** Testing array_unshift() : associative array with different keys ***\n";
  15. //get an unset variable
  16. $unset_var = 10;
  17. unset ($unset_var);
  18. //get a resource variable
  19. $fp = fopen(__FILE__, "r");
  20. //get a class
  21. class classA
  22. {
  23. public function __toString(){
  24. return "Class A object";
  25. }
  26. }
  27. // get a heredoc string
  28. $heredoc = <<<EOT
  29. Hello world
  30. EOT;
  31. // initializing $var argument
  32. $var = 10;
  33. // different variations of associative arrays to be passed to $array argument
  34. $arrays = array (
  35. // empty array
  36. /*1*/ array(),
  37. // arrays with integer keys
  38. array(0 => "0"),
  39. array(1 => "1"),
  40. array(1 => "1", 2 => "2", 3 => "3", 4 => "4"),
  41. // arrays with float keys
  42. /*5*/ array(2.3333 => "float"),
  43. array(1.2 => "f1", 3.33 => "f2",
  44. 4.89999922839999 => "f3",
  45. 33333333.333333 => "f4"),
  46. // arrays with string keys
  47. /*7*/ array('\tHello' => 111, 're\td' => "color",
  48. '\v\fworld' => 2.2, 'pen\n' => 33),
  49. array("\tHello" => 111, "re\td" => "color",
  50. "\v\fworld" => 2.2, "pen\n" => 33),
  51. array("hello", $heredoc => "string"), // heredoc
  52. // array with object, unset variable and resource variable
  53. array(new classA() => 11, @$unset_var => "hello", $fp => 'resource'),
  54. // array with mixed keys
  55. /*11*/ array('hello' => 1, new classA() => 2, "fruit" => 2.2,
  56. $fp => 'resource', 133 => "int", 444.432 => "float",
  57. @$unset_var => "unset", $heredoc => "heredoc")
  58. );
  59. // loop through the various elements of $arrays to test array_unshift()
  60. $iterator = 1;
  61. foreach($arrays as $array) {
  62. echo "-- Iteration $iterator --\n";
  63. /* with default argument */
  64. // returns element count in the resulting array after arguments are pushed to
  65. // beginning of the given array
  66. $temp_array = $array;
  67. var_dump( array_unshift($temp_array, $var) );
  68. // dump the resulting array
  69. var_dump($temp_array);
  70. /* with optional arguments */
  71. // returns element count in the resulting array after arguments are pushed to
  72. // beginning of the given array
  73. $temp_array = $array;
  74. var_dump( array_unshift($temp_array, $var, "hello", 'world') );
  75. // dump the resulting array
  76. var_dump($temp_array);
  77. $iterator++;
  78. }
  79. echo "Done";
  80. ?>
  81. --EXPECTF--
  82. *** Testing array_unshift() : associative array with different keys ***
  83. Warning: Illegal offset type in %s on line %d
  84. Warning: Illegal offset type in %s on line %d
  85. Warning: Illegal offset type in %s on line %d
  86. Warning: Illegal offset type in %s on line %d
  87. -- Iteration 1 --
  88. int(1)
  89. array(1) {
  90. [0]=>
  91. int(10)
  92. }
  93. int(3)
  94. array(3) {
  95. [0]=>
  96. int(10)
  97. [1]=>
  98. string(5) "hello"
  99. [2]=>
  100. string(5) "world"
  101. }
  102. -- Iteration 2 --
  103. int(2)
  104. array(2) {
  105. [0]=>
  106. int(10)
  107. [1]=>
  108. string(1) "0"
  109. }
  110. int(4)
  111. array(4) {
  112. [0]=>
  113. int(10)
  114. [1]=>
  115. string(5) "hello"
  116. [2]=>
  117. string(5) "world"
  118. [3]=>
  119. string(1) "0"
  120. }
  121. -- Iteration 3 --
  122. int(2)
  123. array(2) {
  124. [0]=>
  125. int(10)
  126. [1]=>
  127. string(1) "1"
  128. }
  129. int(4)
  130. array(4) {
  131. [0]=>
  132. int(10)
  133. [1]=>
  134. string(5) "hello"
  135. [2]=>
  136. string(5) "world"
  137. [3]=>
  138. string(1) "1"
  139. }
  140. -- Iteration 4 --
  141. int(5)
  142. array(5) {
  143. [0]=>
  144. int(10)
  145. [1]=>
  146. string(1) "1"
  147. [2]=>
  148. string(1) "2"
  149. [3]=>
  150. string(1) "3"
  151. [4]=>
  152. string(1) "4"
  153. }
  154. int(7)
  155. array(7) {
  156. [0]=>
  157. int(10)
  158. [1]=>
  159. string(5) "hello"
  160. [2]=>
  161. string(5) "world"
  162. [3]=>
  163. string(1) "1"
  164. [4]=>
  165. string(1) "2"
  166. [5]=>
  167. string(1) "3"
  168. [6]=>
  169. string(1) "4"
  170. }
  171. -- Iteration 5 --
  172. int(2)
  173. array(2) {
  174. [0]=>
  175. int(10)
  176. [1]=>
  177. string(5) "float"
  178. }
  179. int(4)
  180. array(4) {
  181. [0]=>
  182. int(10)
  183. [1]=>
  184. string(5) "hello"
  185. [2]=>
  186. string(5) "world"
  187. [3]=>
  188. string(5) "float"
  189. }
  190. -- Iteration 6 --
  191. int(5)
  192. array(5) {
  193. [0]=>
  194. int(10)
  195. [1]=>
  196. string(2) "f1"
  197. [2]=>
  198. string(2) "f2"
  199. [3]=>
  200. string(2) "f3"
  201. [4]=>
  202. string(2) "f4"
  203. }
  204. int(7)
  205. array(7) {
  206. [0]=>
  207. int(10)
  208. [1]=>
  209. string(5) "hello"
  210. [2]=>
  211. string(5) "world"
  212. [3]=>
  213. string(2) "f1"
  214. [4]=>
  215. string(2) "f2"
  216. [5]=>
  217. string(2) "f3"
  218. [6]=>
  219. string(2) "f4"
  220. }
  221. -- Iteration 7 --
  222. int(5)
  223. array(5) {
  224. [0]=>
  225. int(10)
  226. ["\tHello"]=>
  227. int(111)
  228. ["re\td"]=>
  229. string(5) "color"
  230. ["\v\fworld"]=>
  231. float(2.2)
  232. ["pen\n"]=>
  233. int(33)
  234. }
  235. int(7)
  236. array(7) {
  237. [0]=>
  238. int(10)
  239. [1]=>
  240. string(5) "hello"
  241. [2]=>
  242. string(5) "world"
  243. ["\tHello"]=>
  244. int(111)
  245. ["re\td"]=>
  246. string(5) "color"
  247. ["\v\fworld"]=>
  248. float(2.2)
  249. ["pen\n"]=>
  250. int(33)
  251. }
  252. -- Iteration 8 --
  253. int(5)
  254. array(5) {
  255. [0]=>
  256. int(10)
  257. [" Hello"]=>
  258. int(111)
  259. ["re d"]=>
  260. string(5) "color"
  261. [" world"]=>
  262. float(2.2)
  263. ["pen
  264. "]=>
  265. int(33)
  266. }
  267. int(7)
  268. array(7) {
  269. [0]=>
  270. int(10)
  271. [1]=>
  272. string(5) "hello"
  273. [2]=>
  274. string(5) "world"
  275. [" Hello"]=>
  276. int(111)
  277. ["re d"]=>
  278. string(5) "color"
  279. [" world"]=>
  280. float(2.2)
  281. ["pen
  282. "]=>
  283. int(33)
  284. }
  285. -- Iteration 9 --
  286. int(3)
  287. array(3) {
  288. [0]=>
  289. int(10)
  290. [1]=>
  291. string(5) "hello"
  292. ["Hello world"]=>
  293. string(6) "string"
  294. }
  295. int(5)
  296. array(5) {
  297. [0]=>
  298. int(10)
  299. [1]=>
  300. string(5) "hello"
  301. [2]=>
  302. string(5) "world"
  303. [3]=>
  304. string(5) "hello"
  305. ["Hello world"]=>
  306. string(6) "string"
  307. }
  308. -- Iteration 10 --
  309. int(2)
  310. array(2) {
  311. [0]=>
  312. int(10)
  313. [""]=>
  314. string(5) "hello"
  315. }
  316. int(4)
  317. array(4) {
  318. [0]=>
  319. int(10)
  320. [1]=>
  321. string(5) "hello"
  322. [2]=>
  323. string(5) "world"
  324. [""]=>
  325. string(5) "hello"
  326. }
  327. -- Iteration 11 --
  328. int(7)
  329. array(7) {
  330. [0]=>
  331. int(10)
  332. ["hello"]=>
  333. int(1)
  334. ["fruit"]=>
  335. float(2.2)
  336. [1]=>
  337. string(3) "int"
  338. [2]=>
  339. string(5) "float"
  340. [""]=>
  341. string(5) "unset"
  342. ["Hello world"]=>
  343. string(7) "heredoc"
  344. }
  345. int(9)
  346. array(9) {
  347. [0]=>
  348. int(10)
  349. [1]=>
  350. string(5) "hello"
  351. [2]=>
  352. string(5) "world"
  353. ["hello"]=>
  354. int(1)
  355. ["fruit"]=>
  356. float(2.2)
  357. [3]=>
  358. string(3) "int"
  359. [4]=>
  360. string(5) "float"
  361. [""]=>
  362. string(5) "unset"
  363. ["Hello world"]=>
  364. string(7) "heredoc"
  365. }
  366. Done