array_chunk_variation5.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. --TEST--
  2. Test array_chunk() function : usage variations - different 'size' values
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_chunk(array $array, int $size [, bool $preserve_keys])
  6. * Description: Split array into chunks
  7. * : Chunks an array into size large chunks
  8. * Source code: ext/standard/array.c
  9. */
  10. /*
  11. * Testing array_chunk() function with following conditions
  12. * 1. -ve size value
  13. * 2. size value is more than the no. of elements in the input array
  14. * 3. size value is zero
  15. * 4. Decimal size value
  16. */
  17. echo "*** Testing array_chunk() : usage variations ***\n";
  18. // input array
  19. $input_array = array(1, 2, 3);
  20. // different magnitude's
  21. $sizes = array(-1, count($input_array) + 1, 0, 1.5);
  22. // loop through the array for size argument
  23. foreach ($sizes as $size){
  24. echo "\n-- Testing array_chunk() when size = $size --\n";
  25. var_dump( array_chunk($input_array, $size) );
  26. var_dump( array_chunk($input_array, $size, true) );
  27. var_dump( array_chunk($input_array, $size, false) );
  28. }
  29. echo "Done";
  30. ?>
  31. --EXPECTF--
  32. *** Testing array_chunk() : usage variations ***
  33. -- Testing array_chunk() when size = -1 --
  34. Warning: array_chunk(): Size parameter expected to be greater than 0 in %s on line %d
  35. NULL
  36. Warning: array_chunk(): Size parameter expected to be greater than 0 in %s on line %d
  37. NULL
  38. Warning: array_chunk(): Size parameter expected to be greater than 0 in %s on line %d
  39. NULL
  40. -- Testing array_chunk() when size = 4 --
  41. array(1) {
  42. [0]=>
  43. array(3) {
  44. [0]=>
  45. int(1)
  46. [1]=>
  47. int(2)
  48. [2]=>
  49. int(3)
  50. }
  51. }
  52. array(1) {
  53. [0]=>
  54. array(3) {
  55. [0]=>
  56. int(1)
  57. [1]=>
  58. int(2)
  59. [2]=>
  60. int(3)
  61. }
  62. }
  63. array(1) {
  64. [0]=>
  65. array(3) {
  66. [0]=>
  67. int(1)
  68. [1]=>
  69. int(2)
  70. [2]=>
  71. int(3)
  72. }
  73. }
  74. -- Testing array_chunk() when size = 0 --
  75. Warning: array_chunk(): Size parameter expected to be greater than 0 in %s on line %d
  76. NULL
  77. Warning: array_chunk(): Size parameter expected to be greater than 0 in %s on line %d
  78. NULL
  79. Warning: array_chunk(): Size parameter expected to be greater than 0 in %s on line %d
  80. NULL
  81. -- Testing array_chunk() when size = 1.5 --
  82. array(3) {
  83. [0]=>
  84. array(1) {
  85. [0]=>
  86. int(1)
  87. }
  88. [1]=>
  89. array(1) {
  90. [0]=>
  91. int(2)
  92. }
  93. [2]=>
  94. array(1) {
  95. [0]=>
  96. int(3)
  97. }
  98. }
  99. array(3) {
  100. [0]=>
  101. array(1) {
  102. [0]=>
  103. int(1)
  104. }
  105. [1]=>
  106. array(1) {
  107. [1]=>
  108. int(2)
  109. }
  110. [2]=>
  111. array(1) {
  112. [2]=>
  113. int(3)
  114. }
  115. }
  116. array(3) {
  117. [0]=>
  118. array(1) {
  119. [0]=>
  120. int(1)
  121. }
  122. [1]=>
  123. array(1) {
  124. [0]=>
  125. int(2)
  126. }
  127. [2]=>
  128. array(1) {
  129. [0]=>
  130. int(3)
  131. }
  132. }
  133. Done