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