Oihana PHP

shuffle.php

Table of Contents

Functions

shuffle()  : array<string|int, mixed>
Shuffles the elements of an array in place using the Fisher-Yates algorithm.

Functions

shuffle()

Shuffles the elements of an array in place using the Fisher-Yates algorithm.

shuffle(array<string|int, mixed> &$ar) : array<string|int, mixed>

This function randomizes the order of the elements in the given array. It operates directly on the passed array (by reference) and also returns it. The algorithm used ensures an unbiased shuffle.

If the array has 0 or 1 element, it is returned as is without changes.

Parameters
$ar : array<string|int, mixed>

The array to shuffle (passed by reference).

Tags
example
use function oihana\core\arrays\shuffle;

$numbers = [1, 2, 3, 4, 5];
shuffle($numbers);
print_r($numbers); // e.g. [3, 1, 5, 2, 4] - order randomized

$empty = [];
shuffle($empty);
var_dump($empty); // []

$single = [42];
shuffle($single);
var_dump($single); // [42]
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>

The shuffled array.


        
On this page

Search results