I'm wondering what is the easiest/cleanest way to get an array of random values from an array in PHP. It's easy to get an array of random keys but it seems there's not function to get array of values straight away
The easiest way I found out is:
$tokens = ['foo', 'bar', '...'];
$randomValues = array_map(function($k) use ($tokens) {
return $tokens[$k];
}, array_rand($tokens, rand(7, 20)))
This returns 7-20 random values from $tokens
variable. However this looks ugly and is very unclear at first sight what it does.
Aucun commentaire:
Enregistrer un commentaire