PHP Diary | scriptschool.com | PHP Scripts | TD Scripts.com

TD Forum - An unthreaded messageboard script to exchange ideas with your site visitors


[back]go back 07/29/00 "shuffle arrays around the easy way" go forward[next]

Using the built-in shuffle() function

PHP has a neat built-in function called shuffle() that can be used to jumble around the contents of an array for you. I showed you how to shuffle a deck of cards the long way before on 012100, but using the built-in shuffle function we could do it quickly and easily. Here's the same concept revisited using the shuffle function:

<script language="php">
$cards = array("ah", "ac", "ad", "as",
               "2h", "2c", "2d", "2s",
               "3h", "3c", "3d", "3s",
               "4h", "4c", "4d", "4s",
               "5h", "5c", "5d", "5s",
               "6h", "6c", "6d", "6s",
               "7h", "7c", "7d", "7s",
               "8h", "8c", "8d", "8s",
               "9h", "9c", "9d", "9s",
               "th", "tc", "td", "ts",
               "jh", "jc", "jd", "js",
               "qh", "qc", "qd", "qs",
               "kh", "kc", "kd", "ks");

shuffle($cards);
$sizeof = count($cards);
for($index = 0; $index < $sizeof; $index++) {
print(" $cards[$index]");
}
</script>

Can you rewrite the code above to shuffle 5 decks of cards? Try it.

Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)

How useful was this diary entry? Avg Surfer Rating: 3.62 (60)

[back]go back 07/29/00 "shuffle arrays around the easy way" go forward[next]

PHP Diary | scriptschool.com | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 07/29/00 01:20