The first user post under the shuffle documentation:

Shuffle associative and non-associative array while preserving key, value pairs. Also returns the shuffled array instead of shuffling it in place.

function shuffle_assoc($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
} 

Test case:

$arr = array();
$arr[] = array('id' => 5, 'foo' => 'hello');
$arr[] = array('id' => 7, 'foo' => 'byebye');
$arr[] = array('id' => 9, 'foo' => 'foo');
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));
Answer from karim79 on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.shuffle.php
PHP: shuffle - Manual
<?php $numbers = range(1, 20); shuffle($numbers); foreach ($numbers as $number) { echo "$number "; } ?> Note: This function assigns new keys to the elements in array.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_array_shuffle.asp
PHP shuffle() Function
The shuffle() function randomizes the order of the elements in the array. This function assigns new keys for the elements in the array. Existing keys will be removed (See Example below).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-randomize-the-order-of-an-array-in-php
How to Randomize the Order of an Array in PHP ? - GeeksforGeeks
July 23, 2025 - 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 ...
๐ŸŒ
Rust Programming Language
users.rust-lang.org โ€บ help
PHP array shuffle and array_rand replacement - help - The Rust Programming Language Forum
December 20, 2024 - Working on card game deck, where want to get (or sometimes take) random card and also mix the array. In PHP, it's simple by array_rand and shuffle functions but what about Rust? Found few options, where not sure about pโ€ฆ
๐ŸŒ
Lsu
ld2015.scusa.lsu.edu โ€บ php โ€บ function.shuffle.html
Shuffle an array
<?php $numbers = range(1, 20); shuffle($numbers); foreach ($numbers as $number) { echo "$number "; } ?> Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.
๐ŸŒ
PHP Freaks
forums.phpfreaks.com โ€บ php coding โ€บ php coding help
How do I shuffle 2 arrays exactly the same? - PHP Coding Help - PHP Freaks
January 20, 2010 - Hi Guys, Could anyone help me with this problem? I would like to shuffle 2 arrays the same i.e: Before shuffling: array 1: 1, 2, 3, 4, 5 array 2: a, b, c, d, e After shuffling: array 1: 2, 4, 5, 3, 1 array 2: b, d, e, c, a Both arrays index order changed in the same manner. I'm really stumped... ...
Find elsewhere
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ php-cookbook โ€บ 1565926811 โ€บ ch04s21.html
4.20. Randomizing an Array - PHP Cookbook [Book]
November 20, 2002 - You want to scramble the elements of an array in a random order. If youโ€™re running PHP 4.3 or above, use shuffle( ) :
Authors ย  David SklarAdam Trachtenberg
Published ย  2002
Pages ย  640
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-shuffle-function
PHP shuffle() Function - GeeksforGeeks
June 20, 2023 - The shuffle() Function is a builtin function in PHP and is used to shuffle or randomize the order of the elements in an array. This function assigns new keys for the elements in the array.
๐ŸŒ
Hacking with PHP
hackingwithphp.com โ€บ 5 โ€บ 6 โ€บ 10 โ€บ randomising-your-array
Randomising your array: shuffle() and array_rand() โ€“ Hacking with PHP - Practical PHP
Shuffle() takes the entire array and randomises the position of the elements in there. In earlier versions of PHP the shuffle() algorithm was not very good, leading the "randomisation" to be quite weak, however that problem is now fixed.
๐ŸŒ
GitHub
github.com โ€บ deepseek-ai โ€บ DeepSeek-Coder
GitHub - deepseek-ai/DeepSeek-Coder: DeepSeek Coder: Let the Code Write Itself ยท GitHub
The pivot element is then in its final position. The process is then repeated for the sub-arrays.
Author ย  deepseek-ai
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 13.x โ€บ collections
Collections | Laravel 13.x - The clean stack for Artisans and agents
Laravel is a PHP web application framework with expressive, elegant syntax. We've already laid the foundation โ€” freeing you to create without sweating the small things.
๐ŸŒ
ZetCode
zetcode.com โ€บ php-array โ€บ shuffle
PHP shuffle - Array Shuffling in PHP
March 13, 2025 - The function works on both indexed and associative arrays. Syntax: shuffle(array &$array): bool. Note it takes the array by reference and modifies it directly.
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ php-array-exercise-26.php
PHP Array Exercise: Shuffle an associative array, preserving key, value pairs - w3resource
March 31, 2026 - Write a PHP script to use a combination of array_keys() and shuffle() to reorder an associative array and then rebuild it.
๐ŸŒ
Medium
medium.com โ€บ @ok4304571 โ€บ php-shuffle-function-6890066948f9
PHP shuffle() Function - Ok - Medium
July 14, 2025 - PHP shuffle() Function The PHP shuffle( ) function is used to randomize the order of the elements in the array. The function assigns new keys to the elements in an array. This function introduced in โ€ฆ
๐ŸŒ
sebhastian
sebhastian.com โ€บ php-shuffle-array
How to shuffle array in PHP | sebhastian
November 21, 2022 - To shuffle arrays in PHP, you can use the provided shuffle() function.