|
|
A custom PHP function. This function will return an array that’s been resorted in a random order. Supports both numerically-indexed and associative arrays. Uses PHP’s native array_rand() function.- function array_randsort($array,$preserve_keys=false){
- /*-------------------------------------/
- Preserving the keys works best with associative arrays.
- If you choose to preserve keys on a numerically-indexed or
- mixed-indexed array, use a foreach loop rather than a for loop
- to preserve the sorted order.
- /-------------------------------------*/
-
- if(!is_array($array)):
- exit('Supplied argument is not a valid array.');
- else:
- $i = NULL;
-
- // how long is the array?
- $array_length = count($array);
-
- // Sorts the array keys in a random order.
- $randomize_array_keys = array_rand($array,$array_length);
-
- // if we are preserving the keys ...
- if($preserve_keys===true) {
- // reorganize the original array in a new array
- foreach($randomize_array_keys as $k=>$v){
- $randsort[$randomize_array_keys[$k]] = $array[$randomize_array_keys[$k]];
- }
- } else {
- // reorganize the original array in a new array
- for($i=0; $i < $array_length; $i++){
- $randsort[$i] = $array[$randomize_array_keys[$i]];
- }
- }
- return $randsort;
- endif;
- }
复制代码
There is also a shuffle() function in PHP. It randomizes arrays also, but destroys the keys in associative arrays . The other drawback of shuffle() is that it performs its tasks on the actual reference array parameter. So for data integrity a solution like this is probably better.
Copy and paste this script and refresh the page to see the shuffling effect.- <?php
- /**
- * Shuffles and displays cards in a deck
- * @author: Eric Anderson
- * @filename: deckofcards.php
- */
-
- // Create an array of face values
- // and an array of card values
- // then merge them together
- $cards = array_merge(array("J", "Q", "K", "A"), range(2,10)); // 13 cards
-
- // Shuffle the cards
- shuffle($cards);
-
- // Create an multidimentional array to hold the 4 suits
- $suits = array(
- 'Heart' => array(),
- 'Spade' => array(),
- 'Diamond' => array(),
- 'Club' => array()
- );
-
- // Add cards to their respective suits
- 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]."♣";
- }
- }
-
- // Create a deck
- $deck = array();
-
- // Merge the suits into the empty deck array
- $deck = array_merge($deck, $suits);
-
- // Display the deck to the screen
- echo "<p><b>Deck of cards:</b></p>";
- foreach($deck as $k1 => $v1)
- {
- // Display suit name
- echo "<p> $k1's<br /> {<br /> ";
- $acc = 0;
-
- // Display card value
- foreach($v1 as $k2 => $v2)
- {
- echo "$v2 ";
- $acc++;
-
- if ($acc == 4)
- {
- echo "<br /> ";
- $acc = 0;
- }
- }
- echo "<br /> }</p>";
- }
- ?>
复制代码
Tiffany B. Brown
|
|