Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
Random\Randomizer::shuffleArray()
- Get a permutation of an array
Random\Randomizer::shuffleBytes()
- Get a byte-wise permutation of a string
Random\Randomizer::pickArrayKeys()
- Select random array keys
数组排序函数对比
ahmad at ahmadnassri dot com
¶
15 years ago
shuffle for associative arrays, preserves key=>value pairs.
(Based on (Vladimir Kornea of typetango.com)'s function)
<?php
function
shuffle_assoc
(&
$array
) {
$keys
=
array_keys
(
$array
);
shuffle
(
$keys
);
foreach(
$keys
as
$key
) {
$new
[
$key
] =
$array
[
$key
];
}
$array
=
$new
;
return
true
;
}
?>
*note: as of PHP 5.2.10, array_rand's resulting array of keys is no longer shuffled, so we use array_keys + shuffle.
andjones at gmail dot com
¶
14 years ago
Shuffle associative and non-associative array while preserving key, value pairs. Also returns the shuffled array instead of shuffling it in place.
<?php
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
;
}
?>
Anonymous
¶
15 years ago
I needed a simple function two shuffle a two dimensional array. Please note the second level arrays must be indexed using integers, for example $myarray[0]["Name"] and not $myarray["One"]["Name"]. Here is the function:
<?php
function
twodshuffle
(
$array
)
{
$count
=
count
(
$array
);
$indi
=
range
(
0
,
$count
-
1
);
shuffle
(
$indi
);
$newarray
= array(
$count
);
$i
=
0
;
foreach (
$indi
as
$index
)
{
$newarray
[
$i
] =
$array
[
$index
];
$i
++;
}
return
$newarray
;
}
?>
Please note it only works on two dimensional arrays. Here is an example:
<?php
$myarray
= array(
"Google"
=> array(
"Name"
=>
"Google"
,
"URL"
=>
"www.google.com"
,
"Usage"
=>
"Googling"
),
"Yahoo"
=> array(
"Name"
=>
"Yahoo"
,
"URL"
=>
"www.yahoo.com"
,
"Usage"
=>
"Yahooing?"
),
"Ask"
=> array(
"Name"
=>
"Ask"
,
"URL"
=>
"www.ask.com"
,
"Usage"
=>
"Asking Jeeves"
));
print_r
(
twodshuffle
(
$myarray
));
?>
Hope you find it useful!
dirk dot avery a t gmail
¶
15 years ago
Building on examples by m227 and pineappleclock, here is a function that returns all permutations of each set in the power set of an array of strings (instead of a string). Thanks for the great examples!
<?php
function
power_perms
(
$arr
) {
$power_set
=
power_set
(
$arr
);
$result
= array();
foreach(
$power_set
as
$set
) {
$perms
=
perms
(
$set
);
$result
=
array_merge
(
$result
,
$perms
);
}
return
$result
;
}
function
power_set
(
$in
,
$minLength
=
1
) {
$count
=
count
(
$in
);
$members
=
pow
(
2
,
$count
);
$return
= array();
for (
$i
=
0
;
$i
<
$members
;
$i
++) {
$b
=
sprintf
(
"%0"
.
$count
.
"b"
,
$i
);
$out
= array();
for (
$j
=
0
;
$j
<
$count
;
$j
++) {
if (
$b
{
$j
} ==
'1'
)
$out
[] =
$in
[
$j
];
}
if (
count
(
$out
) >=
$minLength
) {
$return
[] =
$out
;
}
}
return
$return
;
}
function
factorial
(
$int
){
if(
$int
<
2
) {
return
1
;
}
for(
$f
=
2
;
$int
-
1
>
1
;
$f
*=
$int
--);
return
$f
;
}
function
perm
(
$arr
,
$nth
=
null
) {
if (
$nth
===
null
) {
return
perms
(
$arr
);
}
$result
= array();
$length
=
count
(
$arr
);
while (
$length
--) {
$f
=
factorial
(
$length
);
$p
=
floor
(
$nth
/
$f
);
$result
[] =
$arr
[
$p
];
array_delete_by_key
(
$arr
,
$p
);
$nth
-=
$p
*
$f
;
}
$result
=
array_merge
(
$result
,
$arr
);
return
$result
;
}
function
perms
(
$arr
) {
$p
= array();
for (
$i
=
0
;
$i
<
factorial
(
count
(
$arr
));
$i
++) {
$p
[] =
perm
(
$arr
,
$i
);
}
return
$p
;
}
function
array_delete_by_key
(&
$array
,
$delete_key
,
$use_old_keys
=
FALSE
) {
unset(
$array
[
$delete_key
]);
if(!
$use_old_keys
) {
$array
=
array_values
(
$array
);
}
return
TRUE
;
}
?>
pineappleclock at gmail dot com
¶
15 years ago
If you want the Power Set (set of all unique subsets) of an array instead of permutations, you can use this simple algorithm:
<?php
function
powerSet
(
$in
,
$minLength
=
1
) {
$count
=
count
(
$in
);
$members
=
pow
(
2
,
$count
);
$return
= array();
for (
$i
=
0
;
$i
<
$members
;
$i
++) {
$b
=
sprintf
(
"%0"
.
$count
.
"b"
,
$i
);
$out
= array();
for (
$j
=
0
;
$j
<
$count
;
$j
++) {
if (
$b
{
$j
} ==
'1'
)
$out
[] =
$in
[
$j
];
}
if (
count
(
$out
) >=
$minLength
) {
$return
[] =
$out
;
}
}
return
$return
;
}
?>
xzero at elite7hackers dot net
¶
9 years ago
There is an function which uses native shuffle() but preserves keys, and their order, so at end, only values are shuffled.
<?PHP
function
array_quake
(&
$array
) {
if (
is_array
(
$array
)) {
$keys
=
array_keys
(
$array
);
$temp
=
$array
;
$array
=
NULL
;
shuffle
(
$temp
);
foreach (
$temp
as
$k
=>
$item
) {
$array
[
$keys
[
$k
]] =
$item
;
}
return;
}
return
false
;
}
$numbers
= array(
'ZERO'
=>
0
,
'ONE'
=>
1
,
'TWO'
=>
2
,
'THREE'
=>
3
,
'FOUR'
=>
4
,
'FIVE'
=>
5
,
'SIX'
=>
6
,
'SEVEN'
=>
7
,
'EIGHT'
=>
8
,
'NINE'
=>
9
);
echo
"\nBefore (original):\n"
;
print_r
(
$numbers
);
array_quake
(
$numbers
);
echo
"\n\nAfter (Array Quake);\n"
;
print_r
(
$numbers
);
echo
"\n"
;
?>
Result example:
Before (original):
Array
(
[ZERO] => 0
[ONE] => 1
[TWO] => 2
[THREE] => 3
[FOUR] => 4
[FIVE] => 5
[SIX] => 6
[SEVEN] => 7
[EIGHT] => 8
[NINE] => 9
)
After (Array Quake);
Array
(
[ZERO] => 4
[ONE] => 2
[TWO] => 0
[THREE] => 8
[FOUR] => 3
[FIVE] => 6
[SIX] => 1
[SEVEN] => 7
[EIGHT] => 5
[NINE] => 9
)
tony at brokerbin dot com
¶
16 years ago
Here is IMO the simplest and extremely fast way to shuffle an associative array AND keep the key=>value relationship. However, it ONLY works if there are NO NUMERIC keys AT ALL. Look into array_merge for the reason why.
<?php
$unshuffled
= array(
'one'
=>
1
,
'two'
=>
2
,
'three'
=>
3
);
$shuffled
=
array_merge
(
array_flip
(
array_rand
(
$unshuffled
,
count
(
$unshuffled
))),
$unshuffled
);
?>
peace
aalaap at gmail dot com
¶
15 years ago
I've been wondering why shuffle() doesn't provide the shuffled array as a return value instead of a bool. I mean, what could possibly go wrong in shuffling elements from an array?
So I use something like this:
<?php
function
array_shuffle
(
$array
) {
if (
shuffle
(
$array
)) {
return
$array
;
} else {
return
FALSE
;
}
}
?>
ezakto at ezakto dot com
¶
14 years ago
This is a replica of shuffle() but preserving keys (associative and non-associative)
bool kshuffle ( array &$array )
<?php
function
kshuffle
(&
$array
) {
if(!
is_array
(
$array
) || empty(
$array
)) {
return
false
;
}
$tmp
= array();
foreach(
$array
as
$key
=>
$value
) {
$tmp
[] = array(
'k'
=>
$key
,
'v'
=>
$value
);
}
shuffle
(
$tmp
);
$array
= array();
foreach(
$tmp
as
$entry
) {
$array
[
$entry
[
'k'
]] =
$entry
[
'v'
];
}
return
true
;
}
$array
= array(
'first'
=>
0
,
'second'
=>
1
,
'third'
=>
2
);
kshuffle
(
$array
);
print_r
(
$array
);
$array
= array(
'first'
,
'second'
,
'third'
);
kshuffle
(
$array
);
print_r
(
$array
);
?>
rick at suggestive dot com
¶
14 years ago
Many people in SEO need to supply an array and shuffle the results and need the same result each time that page is generated. This is my implementation with a working example:
<?php
function
seoShuffle
(&
$items
,
$string
) {
mt_srand
(
strlen
(
$string
));
for (
$i
=
count
(
$items
) -
1
;
$i
>
0
;
$i
--){
$j
= @
mt_rand
(
0
,
$i
);
$tmp
=
$items
[
$i
];
$items
[
$i
] =
$items
[
$j
];
$items
[
$j
] =
$tmp
;
}
}
$items
= array(
'one'
,
'two'
,
'three'
,
'four'
,
'five'
,
'six'
);
$string
=
'whatever'
;
echo
'<pre>'
;
print_r
(
$items
);
echo
'</pre>'
;
seoShuffle
(
$items
,
$string
);
echo
'<pre>'
;
print_r
(
$items
);
echo
'</pre>'
;
?>
sivaji2009 at gmail dot com
¶
15 years ago
Here i wrote a custom shuffle function which preserves the array index and distributes the array element randomly.
<?php
function
custom_shuffle
(
$my_array
= array()) {
$copy
= array();
while (
count
(
$my_array
)) {
$element
=
array_rand
(
$my_array
);
$copy
[
$element
] =
$my_array
[
$element
];
unset(
$my_array
[
$element
]);
}
return
$copy
;
}
$array
= array(
'a'
=>
'apple'
,
'b'
=>
'ball'
,
'c'
=>
'cat'
,
'd'
=>
'dog'
,
'e'
=>
'egg'
,
'f'
=>
'fan'
,
'g'
=>
'gun'
);
print_r
(
custom_shuffle
(
$array
));
Array
(
[
c
] =>
cat
[
e
] =>
egg
[
f
] =>
fan
[
a
] =>
apple
[
b
] =>
ball
[
g
] =>
gun
[
d
] =>
dog
)
?>
Eric Anderson
¶
13 years ago
Copy and paste this script and refresh the page to see the shuffling effect.
<?php
$cards
=
array_merge
(array(
"J"
,
"Q"
,
"K"
,
"A"
),
range
(
2
,
10
));
shuffle
(
$cards
);
$suits
= array(
'Heart'
=> array(),
'Spade'
=> array(),
'Diamond'
=> array(),
'Club'
=> array()
);
for(
$i
=
0
;
$i
<
count
(
$suits
);
$i
++)
{
for(
$j
=
0
;
$j
<
count
(
$cards
);
$j
++)
{
$suits
[
'Heart'
][
$j
] =
$cards
[
$j
].
"<span style=color:#FF0000;>♥</span>"
;
$suits
[
'Spade'
][
$j
] =
$cards
[
$j
].
"♠"
;
$suits
[
'Diamond'
][
$j
] =
$cards
[
$j
].
"<span style=color:#FF0000;>♦</span>"
;
$suits
[
'Club'
][
$j
] =
$cards
[
$j
].
"♣"
;
}
}
$deck
= array();
$deck
=
array_merge
(
$deck
,
$suits
);
echo
"<p><b>Deck of cards:</b></p>"
;
foreach(
$deck
as
$k1
=>
$v1
)
{
echo
"<p> 
$k1
's<br /> {<br />  "
;
$acc
=
0
;
foreach(
$v1
as
$k2
=>
$v2
)
{
echo
"
$v2
 "
;
$acc
++;
if (
$acc
==
4
)
{
echo
"<br />  "
;
$acc
=
0
;
}
}
echo
"<br /> }</p>"
;
}
?>
tyler at CompLangs dot com
¶
15 years ago
Here is a quick function I wrote that generates a random password and uses shuffle() to easily shuffle the order.
<?php
public function
randPass
(
$upper
=
3
,
$lower
=
3
,
$numeric
=
3
,
$other
=
2
) {
$passOrder
= Array();
$passWord
=
''
;
for (
$i
=
0
;
$i
<
$upper
;
$i
++) {
$passOrder
[] =
chr
(
rand
(
65
,
90
));
}
for (
$i
=
0
;
$i
<
$lower
;
$i
++) {
$passOrder
[] =
chr
(
rand
(
97
,
122
));
}
for (
$i
=
0
;
$i
<
$numeric
;
$i
++) {
$passOrder
[] =
chr
(
rand
(
48
,
57
));
}
for (
$i
=
0
;
$i
<
$other
;
$i
++) {
$passOrder
[] =
chr
(
rand
(
33
,
47
));
}
shuffle
(
$passOrder
);
foreach (
$passOrder
as
$char
) {
$passWord
.=
$char
;
}
return
$passWord
;
}
?>
Antonio Ognio
¶
16 years ago
Another shuffle() implementation that preserves keys, does not use extra memory and perhaps is a bit easier to grasp.
<?php
if (
function_exists
(
'shuffle_with_keys'
)===
false
) {
function
shuffle_with_keys
(&
$array
) {
$aux
= array();
$keys
=
array_keys
(
$array
);
shuffle
(
$keys
);
foreach(
$keys
as
$key
) {
$aux
[
$key
] =
$array
[
$key
];
unset(
$array
[
$key
]);
}
$array
=
$aux
;
}
}
?>
DiosDe
¶
3 years ago
implementation of shuffle() using random_int()
<?php
function
random_int_shuffle
( array
$array
): array
{
$array
=
array_values
(
$array
);
$result
= [];
$skip_indexes
= [];
$sizeof_array
=
count
(
$array
);
for (
$i
=
0
;
$i
<
$sizeof_array
;
$i
++ )
{
do
{
$random_index
=
random_int
(
0
,
$sizeof_array
);
} while (
in_array
(
$random_index
,
$skip_indexes
) );
$skip_indexes
[] =
$random_index
;
$result
[] =
$array
[
$random_index
];
}
return
$result
;
}
?>