Random\Randomizer::shuffleArray

(PHP 8 >= 8.2.0)

Random\Randomizer::shuffleArrayGet a permutation of an array

Description

publicRandom\Randomizer::shuffleArray(array$array): array

Returns a uniformly selected permutation of the input array.

Each possible permutation of the input array is equally likely to be returned.

Liste de paramètres

array

The tableau whose values are shuffled.

The input tableau will not be modified.

Valeurs de retour

A permutation of the values of array.

Array keys of the input array will not be preserved; the returned tableau will be a list (array_is_list()).

Erreurs / Exceptions

Exemples

Exemple #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";
?>

Résultat de l'exemple ci-dessus est similaire à :

Salad: 🍎, 🥝, 🍇, 🍌, 🍑 Another Salad: 🍑, 🍇, 🥝, 🍎, 🍌
To Top