PHP shuffle Function | Randomly Shuffle an Array

PHP shuffle is an inbuilt Function in PHP. It shuffles an input array in randomized order using a pseudo number generator. However, it is not suitable for cryptographic purposes. In this article, we will discuss the PHP shuffle Function along with some examples. Also, we will discuss a way to shuffle an associative array.

The internal randomization algorithm has been changed to use the ยป Mersenne Twister Random Number Generator instead of the libc rand function.

PHP.net

Syntax

boolean shuffle(&$array)

Parameters

The PHP shuffle Function expects only one parameter. It specifies an array we want to shuffle.

Return Value

The function returns true on success and false on failure. However, the function does not preserve the original keys of the array. Even if the input array contain string keys, they are lost.

PHP shuffle Function Diagram

Examples

Let’s discuss a few examples to use the shuffle Function.

Example 1: Simple Array

<?php

	$testArray = array(1, 2, 3, 4, 5, 6);
	shuffle($testArray); //Original keys are lost
	print_r($testArray);
	/*
	Array
	(
	    [0] => 4
	    [1] => 6
	    [2] => 5
	    [3] => 3
	    [4] => 2
	    [5] => 1
	)
	*/

	shuffle($testArray); //Original keys are lost
	print_r($testArray);
	/*
	Array
	(
	    [0] => 3
	    [1] => 2
	    [2] => 4
	    [3] => 6
	    [4] => 5
	    [5] => 1
	)
	*/

?>

In the above example, we have a simple array with integers. You can notice, the function shuffles the array in a randomised order. Also, the new array begins with index 0.

Example 2: String Associative Array

For instance, consider an array with string keys.

<?php

	$testArray = array('a' => 'apple', 'b' => 'ball', 'c' => 'cat', 'd' => 'dog');
	shuffle($testArray);
	print_r($testArray);
	/*
	Array
	(
	    [0] => apple
	    [1] => cat
	    [2] => dog
	    [3] => ball
	)
	*/

	shuffle($testArray);
	print_r($testArray);
	/*
	Array
	(
	    [0] => dog
	    [1] => ball
	    [2] => cat
	    [3] => apple
	)
	*/

?>

In the above example, the shuffled array does not retain the original keys of the array. Instead, the new array begins from 0 index.

Pro Tip: Shuffling Associative Array

You can preserve keys in an associative array by using the PHP array_keys Function and shuffle function.

  1. Get the keys of the original array using array_keys Function.
  2. Shuffle the keys array using PHP shuffle.
  3. Get the values from the original array using keys from the shuffled keys array.
<?php 

	$testArray = array('a' => 'apple', 'b' => 'ball', 'c' => 'cat', 'd' => 'dog');
	$keys = array_keys($testArray); //Get the Keys of the array -> a, b, c, d
	shuffle($keys); //Shuffle The keys array -> d, a, c, b
	$shuffledArray = array();
	foreach($keys as $key) {
		$shuffledArray[$key] = $testArray[$key]; //Get the original array using keys from shuffled array
	}
	print_r($shuffledArray);
	/*
	Array
	(
	    [d] => dog
	    [a] => apple
	    [c] => cat
	    [b] => ball
	)
	*/

?>

Conclusion

In conclusion, we discussed the PHP shuffle Function. You can learn more about it on the Official PHP Documentation. Also, you can learn more about PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *