How to Randomize the Order of an Array in PHP ? Last Updated : 05 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Randomizing the order of an array is a common operation in PHP, especially when dealing with datasets or when you want to shuffle the presentation of items. In this article, we will explore various approaches to randomize the order of an array.Table of ContentUsing shuffle() FunctionUsing usort() Function with Custom Comparison FunctionUsing array_rand() FunctionUsing shuffle() FunctionThe shuffle() function is a built-in PHP function specifically designed for randomizing the order of an array. PHP <?php $arr = [1, 2, 3, 4, 5]; // Randomize the order of array // using shuffle() function shuffle($arr); // Display the randomized array print_r($arr); ?> OutputArray ( [0] => 4 [1] => 5 [2] => 2 [3] => 1 [4] => 3 )Using usort() Function with Custom Comparison FunctionThe usort() function allows you to sort an array using a user-defined comparison function. By using a comparison function that returns a random value, you can achieve array randomization. PHP <?php $arr = [1, 2, 3, 4, 5]; // Randomize the order of the array usort($arr, function () { return rand(-1, 1); }); // Display the randomized array print_r($arr); ?> OutputArray ( [0] => 2 [1] => 1 [2] => 3 [3] => 4 [4] => 5 ) Using array_rand() FunctionThe array_rand() function can be used to pick one or more random keys from an array. By using this function, you can shuffle an array by picking random keys and building a new array.Example: PHP <?php $arr = [1, 2, 3, 4, 5]; // Get an array of random keys $randomKeys = array_rand($arr, count($arr)); // Create a new array with values in random order $shuffledArray = []; foreach ($randomKeys as $key) { $shuffledArray[] = $arr[$key]; } // Display the randomized array print_r($shuffledArray); ?> OutputArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )Approach 4: Using array_map() and shuffle()This method combines the use of array_map() with shuffle() to create a shuffled array in a unique way. It involves mapping the original array to a new array of random keys, shuffling these keys, and then reconstructing the array based on the shuffled order of keys.Example: Here’s how you can implement this method to randomize the order of an array: PHP <?php $items = ['apple', 'banana', 'cherry', 'date', 'elderberry']; // Generate a list of keys based on the original array $keys = array_keys($items); // Shuffle the keys shuffle($keys); // Rebuild the array using shuffled keys $shuffledItems = array_map(function ($key) use ($items) { return $items[$key]; }, $keys); print_r($shuffledItems); ?> OutputArray ( [0] => apple [1] => banana [2] => date [3] => elderberry [4] => cherry ) Comment More infoAdvertise with us Next Article How to Check an Array is Sorted or Not in PHP? V vkash8574 Follow Improve Article Tags : PHP PHP-array Geeks Premier League 2023 Similar Reads How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements 4 min read How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte 3 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function 5 min read How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', ' 2 min read How to Shuffle the Elements of an Array in JavaScript? Shuffle an array means randomly change the order of array elements. This is useful for creating random arrangements, like shuffling a deck of cards or randomizing a list. There are different wats to shuffle the elements of an array in JavaScript:1. Using Fisher-Yates (Knuth) ShuffleThis algorithm al 3 min read Like