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