Home  >  Article  >  Backend Development  >  How to randomly swap elements in an array in PHP_PHP Tutorial

How to randomly swap elements in an array in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:53:41966browse

How PHP randomly swaps elements in an array

This article describes how PHP randomly swaps elements in an array. Share it with everyone for your reference. The specific analysis is as follows:

This is a custom PHP function that randomly swaps array elements. PHP already has a built-in function shuffle($Array) with the same function. Please refer to

for this code.

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

// I noticed that there is already a built-in function that

// does the same - so don't use mine ;-)

//

// --> shuffle($Array);

//

// http://de2.php.net/manual/de/function.shuffle.php

//

function RandomizeArray($array){

// error check:

$array = (!is_array($array)) ? array($array) : $array;

$a = array();

$max = count($array) 10;

while(count($array) > 0){

$e = array_shift($array);

$r = rand(0, $max);

// find a empty key:

while (isset($a[$r])){

$r = rand(0, $max);

}

$a[$r] = $e;

}

ksort($a);

$a = array_values($a);

return $a;

}

1

2

3

1

2

3

4

5

6

7

8

9

10

11

12

/*

** Example:

*/

$test_array = array('why','dont','visit','www','jonas','john','de',':-)');

print implode(", ", $test_array);

print "n";

print implode(", ", RandomizeArray($test_array));

/*

Example output:

why, dont, visit, www, jonas, john, de, :-)

www, de, jonas, john, visit, why, :-), dont

*/

4

5

6

8 9 10 11 12
13 14
15 16 17 18 19 20 21 22 23 24 25
// I noticed that there is already a built-in function that // does the same - so don't use mine ;-) // // --> shuffle($Array); // // http://de2.php.net/manual/de/function.shuffle.php // function RandomizeArray($array){ // error check: $array = (!is_array($array)) ? array($array) : $array; $a = array(); $max = count($array) 10; while(count($array) > 0){ $e = array_shift($array); $r = rand(0, $max); // find an empty key: while (isset($a[$r])){ $r = rand(0, $max); } $a[$r] = $e; } ksort($a); $a = array_values($a); return $a; }
Usage example:  ?
1 2 3 4 5 6 7 8 9 10 11 12 /* ** Example: */ $test_array = array('why','dont','visit','www','jonas','john','de',':-)'); print implode(", ", $test_array); print "n"; print implode(", ", RandomizeArray($test_array)); /* Example output: why, dont, visit, www, jonas, john, de, :-) www, de, jonas, john, visit, why, :-), dont */
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/1000104.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000104.htmlTechArticleHow to randomly swap elements in an array in php. This article describes how to randomly swap elements in an array in php. . Share it with everyone for your reference. The specific analysis is as follows: This is...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn