array_randPICK one or more random keys out of an ARRAY.
It allows to request values of INDEXES, in an ARRAY, RANDOMICALLY.
This function uses a pseudo random number generator that is not suitable for cryptographic purposes.
If this function is called with the $array as the only one parameter, $num = 1, returns the key for a random entry, otherwise, an ARRAY of keys for the random entries is returned.
This is done so that random keys can be picked from the ARRAY as well as random values.
To pick more than one element, we have to use $num > 1.
Howerver if $num, is greater than the number of $array elements an E_WARNING level will be issued and NULL will be returned.
Since the PHP 7.1.0 the internal randomization algorithm has been changed to use the Mersenne Twister Random Number Generator instead of the libc rand function.
<?php
mix array_rand ( arr $array [, int $num = 1 ] )
where,
$array = The input ARRAY
$num = The INTEGER to specify how many entries should be picked
?>
$array
The input ARRAY.
$num
To specify how many entries should be picked.
EXERCISE
<?php
$arr01 = ['zero', 'one', 'two', [3, 5, 5]];
print_r($arr01);
$rnd01 = array_rand($arr01);
echo '<br><br>';
echo 'Number of the keys to be picked: 1 ( DEFAULT )<br><br>';
echo 'Randon Number picked: ' . $rnd01;
/* - - - - - - - - - - - - - - - - - - - - - - - -
EVERY NEW EXECUTION ...
DIFFERENT KEY VALUE CAN BE PICKED
- - - - - - - - - - - - - - - - - - - - - - - - */
echo '<br><br>Corresponding element:<br>';
print_r($arr01[$rnd01]);
?>
EXERCISE
<?php
$arr02 = ['zero', 'one', 'two', [3, 5, 5]];
print_r($arr02);
$num02 = 2;
$rnd02 = array_rand($arr02, $num02);
echo '<br><br>';
echo 'Number of the keys to be picked: ' . $num02 . '<br><br>';
echo 'Randon ARRAY picked:<br>';
var_dump($rnd02);
/* - - - - - - - - - - - - - - - - - - - - - - - -
EVERY NEW EXECUTION ...
DIFFERENT KEY VALUE CAN BE PICKED
- - - - - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
$arr03 = ['zero', 'one', 'two', [3, 5, 5]];
print_r($arr03);
$num03 = 4;
$rnd03 = array_rand($arr03, $num03);
echo '<br><br>';
echo 'Number of the keys to be picked: ' . $num03 . '<br><br>';
echo 'Randon ARRAY picked:<br>';
var_dump($rnd03);
/* - - - - - - - - - - - - - - - - - - - - - - - -
EVERY NEW EXECUTION ...
DIFFERENT KEY VALUE CAN BE PICKED
- - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT
ARRAY INITIAL:
Array ( [0] => zero [1] => one [2] => two [3] => Array ( [0] => 3 [1] => 5 [2] => 5 ) )
Number of the keys to be picked:
4
Randon ARRAY picked:
array(4) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) }
EXERCISE
<?php
$arr04 = ['zero', 'one', 'two', [3, 5, 5]];
print_r($arr04);
$num04 = 6;
$rnd04 = array_rand($arr04, $num04);
echo '<br><br>';
echo 'Number of the keys to be picked: ' . $num04 . '<br><br>';
echo 'Randon ARRAY picked:<br>';
var_dump($rnd04);
/* - - - - - - - - - - - - - - - - - - - - - - - -
EVERY NEW EXECUTION ...
DIFFERENT KEY VALUE CAN BE PICKED
- - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT
In PHP 7.4.XX you see a Warning: ...
In PHP 8.0.XX a Fatal error: ... is issued.
You should test on both versions, of course, if possible.
EXERCISE
<?php
$arr05 = [ 1 => 'zero',
2 => 'one', 3 => 'two', 4 => 4 ];
echo 'The given ARRAY:<br>';
print_r($arr05);
echo '<br><br>With the NUMBER of ELEMENTS: ' .
count($arr05) . '<br><br>';
$nbrndx05 = mt_rand(-3, 6);
$rnd05 = array_rand($arr05, $nbrndx05);
echo '<br<br>ENTRIES PICKED: ' . $nbrndx05 .
'<br><br>INDEX VALUE:<br>';
print_r($rnd05);
?>
EXERCISE
<?php
/*
* Test when associative array
* is passed to 'input' argument
*/
echo "Associative array:<br><br>";
// Initialise the 'input' and 'num_req' variables
$input = array(
'one' => 1, 'two' => 2, 'three' => 3,
'FoUr' => 'four', '#5' => 5, 'SIX' => 'six',
"seven" => 7, "#8" => "eight", "nine" => "NINE"
);
$num_req = 6;
// Calling array_rand() with optional argument
echo"With all default and optional arguments:<br>";
var_dump($input);
echo '<br>Index Value:<br>';
var_dump( array_rand($input,$num_req) );
// Calling array_rand() with default arguments
echo"<br><br>With default argument:<br>";
var_dump($input);
echo '<br>Index Value:<br>';
var_dump( array_rand($input) );
?>
EXERCISE
<?php
/*
* Test behaviour of this function
* when multi-dimensional array
* is passed to 'input' argument
*/
echo "With multi-dimensional array:<br><br>";
// initialise the multi-dimensional array
$input = array(
// array with int values
/*1*/ array(1, 2, 0, -0, -1, -2),
// array with float values
array(1.23, -1.23, 0.34, -0.34, 0e2, 2e-3, -2e2, -40e-2),
// array with single quoted strings
/*3*/ array('one', '123numbers', 'hello\tworld',
'hello world\0', '12.34floatnum'),
// array with double quoted strings
array("one","123numbers", "hello\tworld",
"hello world\0", "12.34floatnum"),
// array with bool values
/*5*/ array(true, TRUE, FALSE, false, TrUe, FaLsE),
// array with hexa values
array(0x123, -0x123, 0xabc, 0xABC, 0xab),
// array with null values
/*7*/ array(null, NULL, "\0", Null, NuLl)
);
print_r($input);
echo '<br><br>';
// initialise 'num_req' variable
$num_req = 3;
echo '<br<br>ENTRIES PICKED: ' . $num_req .
'<br><br>INDEX VALUE:<br>';
// calling array_rand() function
// with multi-dimensional array
var_dump( array_rand($input, $num_req) );
// looping to test array_rand()
// with each sub-array in the multi-dimensional array
echo "<br><br><br>With arrays having different types of values: ";
$counter = 1;
foreach($input as $arr) {
echo "<br><br>Iteration: $counter<br><br>";
var_dump( array_rand($arr) );
// with default arguments
var_dump( array_rand($arr, 3) );
// with default as well as optional arguments
$counter++;
}
?>
EXERCISE
<?php
/*
* Test behaviour of this function
* when associative array is passed to
* the 'input' parameter in the function call
*/
echo "With associative arrays:<br><br>";
// 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, 012 => 10, 010 => "8",
-034 => "-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 "<br>Iteration: $counter<br>";
// with default argument
echo"<br>With default argument:<br>";
var_dump( array_rand($input) );
// with default and optional arguments
echo"<br><br>With num_req = 1<br>";
var_dump( array_rand($input, 1) );
// with $num_req=1
echo"<br><br>With num_req = 2<br>";
var_dump( array_rand($input, 2) );
// with $num_req=2
$counter++;
} // end of for loop
?>
EXERCISE
<?php
/*
* Test behaviour of this function
* when keys of the 'input' array
* is heredoc string
*/
echo "With keys of input array as heredoc strings:<br>";
// defining different heredoc strings
$empty_heredoc = <<<EOT
EOT;
$heredoc_with_newline = <<<EOT
\n
EOT;
$heredoc_with_characters = <<<EOT
first line of heredoc string
second line of heredoc string
third line of heredocstring
EOT;
$heredoc_with_newline_and_tabs = <<<EOT
hello\tworld\nhello\nworld\n
EOT;
$heredoc_with_alphanumerics = <<<EOT
hello123world456
1234hello\t1234
EOT;
$heredoc_with_embedded_nulls = <<<EOT
hello\0world\0hello
\0hello\0
EOT;
$input = array(
$empty_heredoc => "heredoc1",
$heredoc_with_newline => "heredoc2",
$heredoc_with_characters => "heredoc3",
$heredoc_with_newline_and_tabs => "heredoc3",
$heredoc_with_alphanumerics => "heredoc4",
$heredoc_with_embedded_nulls => "heredoc5"
);
// Test array_rand() function with
// different valid 'req_num' values
echo "<br><br>With default parameters<br>";
var_dump( array_rand($input) );
echo "<br><br>With num_req = 1<br>";
var_dump( array_rand($input, 1) );
echo "<br><br>With num_req = 3<br>";
var_dump( array_rand($input, 3) );
echo "<br><br>With num_req = 6<br>";
var_dump( array_rand($input, 6) );
?>