Random\Randomizer::shuffleArray
(PHP 8 >= 8.2.0)
Random\Randomizer::shuffleArray — Get a permutation of an array
Descrição
Returns a uniformly selected permutation of the input array
.
Each possible permutation of the input array
is equally likely to be returned.
Valor Retornado
A permutation of the values of array
.
Array keys of the input array
will not be preserved;
the returned array will be a list (array_is_list()).
Erros/Exceções
-
Qualquer erro ou exceção Throwable lançados pelo método Random\Engine::generate()
do mecanismo
Random\Randomizer::$engine
subjacente.
Exemplos
Example #1 Random\Randomizer::shuffleArray() example
<?php
$r = new \Random\Randomizer();
$fruits = [ 'red' => '🍎', 'green' => '🥝', 'yellow' => '🍌', 'pink' => '🍑', 'purple' => '🍇' ];
// Shuffle array:
echo "Salad: ", implode(', ', $r->shuffleArray($fruits)), "\n";
// Shuffle again:
echo "Another Salad: ", implode(', ', $r->shuffleArray($fruits)), "\n";
?>
O exemplo acima produzirá algo semelhante a:
Salad: 🍎, 🥝, 🍇, 🍌, 🍑 Another Salad: 🍑, 🍇, 🥝, 🍎, 🍌