@ 2024-01-25T10:07:53Z <?php
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"]));
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.2 0.017 0.008 25.66 8.3.1 0.003 0.016 25.66 8.3.0 0.014 0.005 25.66 8.2.15 0.019 0.007 25.66 8.2.14 0.015 0.003 25.66 8.2.13 0.014 0.005 25.66 8.2.12 0.011 0.009 25.66 8.2.11 0.014 0.006 25.66 8.2.10 0.011 0.011 25.66 8.2.9 0.018 0.007 25.66 8.2.8 0.009 0.012 25.66 8.2.7 0.018 0.007 25.66 8.2.6 0.010 0.009 25.66 8.2.5 0.016 0.009 25.66 8.2.4 0.017 0.005 25.66 8.2.3 0.012 0.007 25.66 8.2.2 0.012 0.008 25.66 8.2.1 0.017 0.007 25.66 8.2.0 0.009 0.008 25.66 8.1.27 0.014 0.003 25.66 8.1.26 0.012 0.007 25.66 8.1.25 0.005 0.014 25.66 8.1.24 0.014 0.008 25.66 8.1.23 0.013 0.010 25.66 8.1.22 0.013 0.011 25.66 8.1.21 0.019 0.005 25.66 8.1.20 0.009 0.010 25.66 8.1.19 0.013 0.005 25.66 8.1.18 0.015 0.007 25.66 8.1.17 0.013 0.007 25.66 8.1.16 0.019 0.003 25.66 8.1.15 0.010 0.008 25.66 8.1.14 0.018 0.006 25.66 8.1.13 0.011 0.009 25.66 8.1.12 0.016 0.004 25.66 8.1.11 0.013 0.005 25.66 8.1.10 0.012 0.007 25.66 8.1.9 0.008 0.011 25.66 8.1.8 0.008 0.009 25.66 8.1.7 0.011 0.008 25.66 8.1.6 0.016 0.002 25.66 8.1.5 0.015 0.005 25.66 8.1.4 0.012 0.003 25.66 8.1.3 0.015 0.011 25.66 8.1.2 0.017 0.008 25.66 8.1.1 0.020 0.005 25.66 8.1.0 0.012 0.003 25.66
preferences:dark mode live preview ace vim emacs key bindings
35.13 ms | 403 KiB | 5 Q