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