123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- --TEST--
- Test array_rand() function : usage variation - with associative arrays for 'input' parameter
- --FILE--
- <?php
- /* Prototype : mixed array_rand(array $input [, int $num_req])
- * Description: Return key/keys for random entry/entries in the array
- * Source code: ext/standard/array.c
- */
- /*
- * Test behaviour of array_rand() function when associative array is passed to
- * the 'input' parameter in the function call
- */
- echo "*** Testing array_rand() : with associative arrays ***\n";
- // initialise associative arrays
- $asso_arrays = array(
-
- // array with numeric keys
- /*1*/ array(1 => 'one', 2 => 2, 1234567890 => 'big', -1 => 'negative key',
- 2.3 => 'float key', 0 => "zero key", 0.2 => 'decimal key',
- 2e2 => 'exp key1', -2e3 => 'negative exp key'),
-
- // array with string keys
- array('one' => 1, "two" => 2.0, "three" => 'three',
- '12twelve' => 12.00, "" => 'empty string', " " => "space key"),
- // array with hexa values as keys
- /*3*/ array(0xabc => 2748, 0x12f => '303', 0xff => "255", -0xff => "-255"),
- // array with octal values as keys
- array(0123 => 83, 0129 => 10, 010 => "8", -0348 => "-28", 0012 => '10'),
- // array with bool values as keys
- array(TRUE => '1', true => true, TrUe => "TRUE",
- FALSE => '0', false => false, FaLsE => "FALSE"),
- // array with special chars as keys
- /*6*/ array('##' => "key1", '&$r' => 'key2', '!' => "key3", '<>' =>'key4',
- "NULL" => 'key5', "\n" => 'newline as key',
- "\t" => "tab as key", "'" => 'single quote as key',
- '"' => 'double quote as key', "\0" => "null char as key")
- );
-
- /* looping to test array_rand() function with different arrays having
- * different types of keys
- */
- $counter = 1;
- foreach($asso_arrays as $input) {
- echo "\n-- Iteration $counter --\n";
- // with default argument
- echo"\nWith default argument\n";
- var_dump( array_rand($input) );
- // with default and optional arguments
- echo"\nWith num_req = 1\n";
- var_dump( array_rand($input, 1) ); // with $num_req=1
- echo"\nWith num_req = 2\n";
- var_dump( array_rand($input, 2) ); // with $num_req=2
-
- $counter++;
- } // end of for loop
-
- echo "Done";
- ?>
- --EXPECTREGEX--
- \*\*\* Testing array_rand\(\) : with associative arrays \*\*\*
- -- Iteration 1 --
- With default argument
- int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
- With num_req = 1
- int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
- With num_req = 2
- array\(2\) {
- \[0\]=>
- int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
- \[1\]=>
- int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
- }
- -- Iteration 2 --
- With default argument
- string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
- With num_req = 1
- string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
- With num_req = 2
- array\(2\) {
- \[0\]=>
- string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
- \[1\]=>
- string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
- }
- -- Iteration 3 --
- With default argument
- int\([23-]*[2570]*[345]*[58]*\)
- With num_req = 1
- int\([23-]*[2570]*[345]*[58]*\)
- With num_req = 2
- array\(2\) {
- \[0\]=>
- int\([23-]*[2570]*[345]*[58]*\)
- \[1\]=>
- int\([23-]*[2570]*[345]*[58]*\)
- }
- -- Iteration 4 --
- With default argument
- int\([18-]*[023]*[8]*\)
- With num_req = 1
- int\([18-]*[023]*[8]*\)
- With num_req = 2
- array\(2\) {
- \[0\]=>
- int\([18-]*[023]*[8]*\)
- \[1\]=>
- int\([18-]*[023]*[8]*\)
- }
- -- Iteration 5 --
- With default argument
- int\([01]\)
- With num_req = 1
- int\([01]\)
- With num_req = 2
- array\(2\) {
- \[0\]=>
- int\([01]\)
- \[1\]=>
- int\([01]\)
- }
- -- Iteration 6 --
- With default argument
- string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
- With num_req = 1
- string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
- With num_req = 2
- array\(2\) {
- \[0\]=>
- string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
- \[1\]=>
- string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
- }
- Done
|