note 129194 added to function.array-rand
| From: | ray dot kunal11323 at gmail dot com | Date: | Thu, 25 Jan 2024 10:35:53 +0000 |
| Subject: | note 129194 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221148@lists.php.net to get a copy of this message | ||
for a cryptographically secure version, try this
It's a better version. It's almost similar to the 'array_rand', it accepts both
'$array' and '$num' to pick one or more random keys out of an array. Also, all
the validations are included in this function.
<?php
/**
* Pick one or more random keys out of an array, using a cryptograpically secure rng
* discussed+reviewed at https://codereview.stackexchange.com/a/289121/279740
*
* @param array $array
* @param int $num = 1
* @return int|string|array|null
*/
function array_rand_secure($array, $num = 1)
{
if (!is_array($array)) {
trigger_error(
__FUNCTION__ . "(): expects parameter 1 to be array, " . gettype($array) .
" given",
E_USER_WARNING
);
return null;
}
$arrayLength = count($array);
if ($arrayLength === 0) {
trigger_error(__FUNCTION__ . "(): Array is empty", E_USER_WARNING);
return null;
}
if (
filter_var($num, FILTER_VALIDATE_INT) === false
|| $num <= 0
|| ($arrayLength - $num) < 0
) {
trigger_error(
__FUNCTION__ . "(): Second argument has to be between 1 and the number of
elements in the array",
E_USER_WARNING);
return null;
}
$randomIndexes = [];
while(count($randomIndexes) < $num) {
$randomInt = random_int(0, $arrayLength - 1);
if (!in_array($randomInt, $randomIndexes)) $randomIndexes[] = $randomInt;
};
$arrayKeys = array_keys($array);
$randomKeys = array_map(function ($key) use ($arrayKeys) {
return $arrayKeys[$key];
}, $randomIndexes);
return count($randomKeys) === 1 ? $randomKeys[0] : $randomKeys;
}
$tests1 = [
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[5, 6, 7],
['a' => 1, 'b' => 2, 'c' => 3],
['zero', 4 => 'four', 9 => 'nine'],
["PEAN"=>0],
[],
"ABC",
123,
];
foreach ($tests1 as $test) {
var_dump(array_rand_secure($test));
}
$tests2 = [
["array" => [1, 2, 3, 4, 5, 6, 7, 8, 9], "num" => 9],
["array" => [1, 2, 3, 4, 5, 6, 7, 8, 9], "num" => 4],
["array" => ['a' => 1, 'b' => 2, 'c' =>
3], "num" => 2],
["array" => ['zero', 4 => 'four', 9 =>
'nine'], "num" => 2],
["array" => ["PEAN" => 0], "num" => 2],
["array" => [], "num" => 2],
["array" => "ABC", "num" => 2],
["array" => 123, "num" => 2],
["array" => [5, 6, 7], "num" => 0],
];
foreach ($tests2 as $test_data) {
var_dump(array_rand_secure($test_data["array"], $test_data["num"]));
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 122.163.121.189)
----
Manual Page -- https://php.net/manual/en/function.array-rand.php
Edit -- https://main.php.net/note/edit/129194
Del: integrated -- https://main.php.net/note/delete/129194/integrated
Del: useless -- https://main.php.net/note/delete/129194/useless
Del: bad code -- https://main.php.net/note/delete/129194/bad+code
Del: spam -- https://main.php.net/note/delete/129194/spam
Del: non-english -- https://main.php.net/note/delete/129194/non-english
Del: in docs -- https://main.php.net/note/delete/129194/in+docs
Del: other reasons-- https://main.php.net/note/delete/129194
Reject -- https://main.php.net/note/reject/129194
Search -- https://main.php.net/manage/user-notes.php