3v4l.org

run code in 300+ PHP versions simultaneously
<?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"])); }

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).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.20.0170.00825.66
8.3.10.0030.01625.66
8.3.00.0140.00525.66
8.2.150.0190.00725.66
8.2.140.0150.00325.66
8.2.130.0140.00525.66
8.2.120.0110.00925.66
8.2.110.0140.00625.66
8.2.100.0110.01125.66
8.2.90.0180.00725.66
8.2.80.0090.01225.66
8.2.70.0180.00725.66
8.2.60.0100.00925.66
8.2.50.0160.00925.66
8.2.40.0170.00525.66
8.2.30.0120.00725.66
8.2.20.0120.00825.66
8.2.10.0170.00725.66
8.2.00.0090.00825.66
8.1.270.0140.00325.66
8.1.260.0120.00725.66
8.1.250.0050.01425.66
8.1.240.0140.00825.66
8.1.230.0130.01025.66
8.1.220.0130.01125.66
8.1.210.0190.00525.66
8.1.200.0090.01025.66
8.1.190.0130.00525.66
8.1.180.0150.00725.66
8.1.170.0130.00725.66
8.1.160.0190.00325.66
8.1.150.0100.00825.66
8.1.140.0180.00625.66
8.1.130.0110.00925.66
8.1.120.0160.00425.66
8.1.110.0130.00525.66
8.1.100.0120.00725.66
8.1.90.0080.01125.66
8.1.80.0080.00925.66
8.1.70.0110.00825.66
8.1.60.0160.00225.66
8.1.50.0150.00525.66
8.1.40.0120.00325.66
8.1.30.0150.01125.66
8.1.20.0170.00825.66
8.1.10.0200.00525.66
8.1.00.0120.00325.66

preferences:
35.13 ms | 403 KiB | 5 Q