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 OverflowPHP
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).
7: How shuffle a PHP array - PHP 7 tutorial
05:31
How to get random items from an array using php. - YouTube
03:04
Array Shuffle Function In PHP - YouTube
04:05
How to SHUFFLE AN ARRAY in 4 minutes! ๐ - YouTube
05:10
Arrays - ZUFALL shuffle(), array_rand() [PHP Doku - Funktionsr...
06:52
7: How shuffle a PHP array - PHP 7 tutorial - YouTube
Top answer 1 of 10
99
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));
2 of 10
25
As of 5.3.0 you could do:
uksort($array, function() { return rand() > rand(); });
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... ...
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.
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.
Facebook
facebook.com โบ edmmaniac โบ posts โบ carter-faith-is-entering-the-edm-space-with-her-breakout-collaboration-alongside โบ 1254994833493992
Carter Faith is entering the EDM space with her breakout ...
We cannot provide a description for this page right now
Educative
educative.io โบ answers โบ what-is-the-array-shuffle-method-in-php
What is the array shuffle() method in PHP?
We used the shuffle method to shuffle the position of the elements of the array.