array_rand_variation4.phpt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. --TEST--
  2. Test array_rand() function : usage variation - with associative arrays for 'input' parameter
  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 associative array is passed to
  11. * the 'input' parameter in the function call
  12. */
  13. echo "*** Testing array_rand() : with associative arrays ***\n";
  14. // initialise associative arrays
  15. $asso_arrays = array(
  16. // array with numeric keys
  17. /*1*/ array(1 => 'one', 2 => 2, 1234567890 => 'big', -1 => 'negative key',
  18. 2.3 => 'float key', 0 => "zero key", 0.2 => 'decimal key',
  19. 2e2 => 'exp key1', -2e3 => 'negative exp key'),
  20. // array with string keys
  21. array('one' => 1, "two" => 2.0, "three" => 'three',
  22. '12twelve' => 12.00, "" => 'empty string', " " => "space key"),
  23. // array with hexa values as keys
  24. /*3*/ array(0xabc => 2748, 0x12f => '303', 0xff => "255", -0xff => "-255"),
  25. // array with octal values as keys
  26. array(0123 => 83, 0129 => 10, 010 => "8", -0348 => "-28", 0012 => '10'),
  27. // array with bool values as keys
  28. array(TRUE => '1', true => true, TrUe => "TRUE",
  29. FALSE => '0', false => false, FaLsE => "FALSE"),
  30. // array with special chars as keys
  31. /*6*/ array('##' => "key1", '&$r' => 'key2', '!' => "key3", '<>' =>'key4',
  32. "NULL" => 'key5', "\n" => 'newline as key',
  33. "\t" => "tab as key", "'" => 'single quote as key',
  34. '"' => 'double quote as key', "\0" => "null char as key")
  35. );
  36. /* looping to test array_rand() function with different arrays having
  37. * different types of keys
  38. */
  39. $counter = 1;
  40. foreach($asso_arrays as $input) {
  41. echo "\n-- Iteration $counter --\n";
  42. // with default argument
  43. echo"\nWith default argument\n";
  44. var_dump( array_rand($input) );
  45. // with default and optional arguments
  46. echo"\nWith num_req = 1\n";
  47. var_dump( array_rand($input, 1) ); // with $num_req=1
  48. echo"\nWith num_req = 2\n";
  49. var_dump( array_rand($input, 2) ); // with $num_req=2
  50. $counter++;
  51. } // end of for loop
  52. echo "Done";
  53. ?>
  54. --EXPECTREGEX--
  55. \*\*\* Testing array_rand\(\) : with associative arrays \*\*\*
  56. -- Iteration 1 --
  57. With default argument
  58. int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
  59. With num_req = 1
  60. int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
  61. With num_req = 2
  62. array\(2\) {
  63. \[0\]=>
  64. int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
  65. \[1\]=>
  66. int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
  67. }
  68. -- Iteration 2 --
  69. With default argument
  70. string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
  71. With num_req = 1
  72. string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
  73. With num_req = 2
  74. array\(2\) {
  75. \[0\]=>
  76. string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
  77. \[1\]=>
  78. string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
  79. }
  80. -- Iteration 3 --
  81. With default argument
  82. int\([23-]*[2570]*[345]*[58]*\)
  83. With num_req = 1
  84. int\([23-]*[2570]*[345]*[58]*\)
  85. With num_req = 2
  86. array\(2\) {
  87. \[0\]=>
  88. int\([23-]*[2570]*[345]*[58]*\)
  89. \[1\]=>
  90. int\([23-]*[2570]*[345]*[58]*\)
  91. }
  92. -- Iteration 4 --
  93. With default argument
  94. int\([18-]*[023]*[8]*\)
  95. With num_req = 1
  96. int\([18-]*[023]*[8]*\)
  97. With num_req = 2
  98. array\(2\) {
  99. \[0\]=>
  100. int\([18-]*[023]*[8]*\)
  101. \[1\]=>
  102. int\([18-]*[023]*[8]*\)
  103. }
  104. -- Iteration 5 --
  105. With default argument
  106. int\([01]\)
  107. With num_req = 1
  108. int\([01]\)
  109. With num_req = 2
  110. array\(2\) {
  111. \[0\]=>
  112. int\([01]\)
  113. \[1\]=>
  114. int\([01]\)
  115. }
  116. -- Iteration 6 --
  117. With default argument
  118. string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
  119. With num_req = 1
  120. string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
  121. With num_req = 2
  122. array\(2\) {
  123. \[0\]=>
  124. string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
  125. \[1\]=>
  126. string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
  127. }
  128. Done