Sample Array

Get N random items from an array without replacement.

Code

General
$keys = array_rand($arr, $n);
return array_map(fn($k) => $arr[$k], (array)$keys);

Parameters

Array to sample from

Number of items to sample

Server

More PHP Snippets