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