array_rand_variation3.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. --TEST--
  2. Test array_rand() function : usage variation - with MultiDimensional array
  3. --FILE--
  4. <?php
  5. /* Prototype : mixed array_rand(array $input [, int $num_req])
  6. * Description: Return key/keys for random entry/entries in the array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Test behaviour of array_rand() function when multi-dimensional array
  11. * is passed to 'input' argument
  12. */
  13. echo "*** Testing array_rand() : with multi-dimensional array ***\n";
  14. // initialise the multi-dimensional array
  15. $input = array(
  16. // array with int values
  17. /*1*/ array(1, 2, 0, -0, -1, -2),
  18. // array with float values
  19. array(1.23, -1.23, 0.34, -0.34, 0e2, 2e-3, -2e2, -40e-2),
  20. // array with single quoted strings
  21. /*3*/ array('one', '123numbers', 'hello\tworld', 'hello world\0', '12.34floatnum'),
  22. // array with double quoted strings
  23. array("one","123numbers", "hello\tworld", "hello world\0", "12.34floatnum"),
  24. // array with bool values
  25. /*5*/ array(true, TRUE, FALSE, false, TrUe, FaLsE),
  26. // array with hexa values
  27. array(0x123, -0x123, 0xabc, 0xABC, 0xab),
  28. // array with null values
  29. /*7*/ array(null, NULL, "\0", Null, NuLl)
  30. );
  31. // initialise 'num_req' variable
  32. $num_req = 3;
  33. // calling array_rand() function with multi-dimensional array
  34. var_dump( array_rand($input, $num_req) );
  35. // looping to test array_rand() with each sub-array in the multi-dimensional array
  36. echo "\n*** Testing array_rand() with arrays having different types of values ***\n";
  37. $counter = 1;
  38. foreach($input as $arr) {
  39. echo "\n-- Iteration $counter --\n";
  40. var_dump( array_rand($arr) ); // with default arguments
  41. var_dump( array_rand($arr, 3) ); // with default as well as optional arguments
  42. $counter++;
  43. }
  44. echo "Done";
  45. ?>
  46. --EXPECTF--
  47. *** Testing array_rand() : with multi-dimensional array ***
  48. array(3) {
  49. [0]=>
  50. int(%d)
  51. [1]=>
  52. int(%d)
  53. [2]=>
  54. int(%d)
  55. }
  56. *** Testing array_rand() with arrays having different types of values ***
  57. -- Iteration 1 --
  58. int(%d)
  59. array(3) {
  60. [0]=>
  61. int(%d)
  62. [1]=>
  63. int(%d)
  64. [2]=>
  65. int(%d)
  66. }
  67. -- Iteration 2 --
  68. int(%d)
  69. array(3) {
  70. [0]=>
  71. int(%d)
  72. [1]=>
  73. int(%d)
  74. [2]=>
  75. int(%d)
  76. }
  77. -- Iteration 3 --
  78. int(%d)
  79. array(3) {
  80. [0]=>
  81. int(%d)
  82. [1]=>
  83. int(%d)
  84. [2]=>
  85. int(%d)
  86. }
  87. -- Iteration 4 --
  88. int(%d)
  89. array(3) {
  90. [0]=>
  91. int(%d)
  92. [1]=>
  93. int(%d)
  94. [2]=>
  95. int(%d)
  96. }
  97. -- Iteration 5 --
  98. int(%d)
  99. array(3) {
  100. [0]=>
  101. int(%d)
  102. [1]=>
  103. int(%d)
  104. [2]=>
  105. int(%d)
  106. }
  107. -- Iteration 6 --
  108. int(%d)
  109. array(3) {
  110. [0]=>
  111. int(%d)
  112. [1]=>
  113. int(%d)
  114. [2]=>
  115. int(%d)
  116. }
  117. -- Iteration 7 --
  118. int(%d)
  119. array(3) {
  120. [0]=>
  121. int(%d)
  122. [1]=>
  123. int(%d)
  124. [2]=>
  125. int(%d)
  126. }
  127. Done