PHP _ PHP Tutorial

Source: Internet
Author: User
PHP array for details. With the development of the times, we have more and more programming technologies. for PHP, everyone must be familiar with it. let's introduce the PHP array here. PHP array is the most common batch data storage. with the development of the times, more and more programming technologies are available. for PHP, you must be familiar with it. let's introduce the PHP array here. PHP arrays are the most common batch data storage structures. Because arrays are stored in sequence, they can be stored randomly conveniently. In many languages, arrays are static and must be specified before use. in PHP, you do not have to worry about this problem. arrays are completely dynamic. You can even use an array like this.

 
  1. PHP CODE
  2. // DEMO of Array
  3. demo_array[] = 1;
  4. demo_array[] = 2;

Next we will use the PHP array to simulate the shuffling of mahjong, a sports favorite of the majority of Chinese working people. Here we will popularize the Mahjong rules, which will be useful when serving MM's mother later :). First of all, mahjong is intended for 4 people, but it cannot be played by 3 p. There are a total of 108 cards. after you touch the cards, there are 14 at the dealer's house, and 13 at the other three, with 55 cards left. (There are N ways to play mahjong in China. here we are talking about Easy home games. it is the simplest way to play mahjong games that I have ever seen .) Then let's analyze how to use a random number to simulate this process. this kind of practice can be done, because extracting a mathematical model from a specific example is the basic skill of programming. First, we use an array with a length of 108 to store these 108 cards. during initialization, we may assume they are discharged sequentially.

Then we will simulate the shuffling process, which is actually a process of two card exchanges several times. We generate two ~ The random number between 108, and then the value of the array element with these two numbers as index is exchanged. We can define this process as a function for multiple exchanges. Next, we will distribute the 108 cards to four people. In fact, as long as the number of exchanges on the front side is large enough, it will be OK to cut them into five parts directly. However, in order to better simulate the reality and present the characteristics of the array, we adopt a circular distribution method. That is, each person has four images at a time, one for each person, and two for the banker. Well, the whole process is like this. Below we will explain some more details through the program.

 
  1. PHP CODE
  2. Php
  3. /*************************************** ***********************
  4. * An example of using arrays to simulate mahjong shuffling and card issuing processes
  5. * @ Easy@bjpeu.edu.cn
  6. * @ 2003-6-3
  7. **************************************** ***********************/
  8. // Card swap function
  9. Function swap ()
  10. {
  11. // Define $ majiangArray as a global variable
  12. // You can perform operations in the function.
  13. Global $ majiangArray;
  14. // Generate two random numbers ranging from 1 to 108
  15. $Index1=Rand(1,108 );
  16. $Index2=Rand(1,108 );
  17. // Exchange the values of corresponding array elements
  18. $Tmp= $ MajiangArray [$ index1];
  19. $ MajiangArray [$ index1] = $ majiangArray [$ index2];
  20. $ MajiangArray [$ index2] = $ tmp;
  21. }
  22. // Functions that represent the array content with the corresponding image
  23. Function showArray ($ arrayName)
  24. {
  25. $CountCount= Count ($ arrayName );
  26. For ($I=0; $ I<$ Count; $ I ++)
  27. {
  28. Echo'<Img Src="Img/'. $ arrayName [$ I].'">'; // Display the corresponding image
  29. }
  30. Echo'<P>'; // Line feed
  31. }
  32. // The main program starts.
  33. // Initialize the array
  34. Unset ($ majiangArray );
  35. $ MajiangArray [] = '0'; // data is stored from 1, skipping 0
  36. For ($I=1; $ I<= 3; $ I ++) // There are three colors in total
  37. {
  38. For ($J=1; $ J<= 9; $ j ++) // There are nine cards in a suit
  39. {
  40. For ($K=1; $ K<= 4; $ k ++) // Each card has four
  41. {
  42. $ MajiangArray [] = $ I. '_'. $ j. '.gif ';
  43. }
  44. }
  45. }
  46. // Start shuffling
  47. For ($I=1; $ I<= 1000; $ I ++) // The cyclic variable I is re-assigned and reused.
  48. {
  49. Swap ();
  50. }
  51. // Initialize four users
  52. Unset ($ user1 );
  53. Unset ($ user2 );
  54. Unset ($ user3 );
  55. Unset ($ user4 );
  56. // $ Seek is the progress pointer. The record should be licensed from there.
  57. $Seek=1;
  58. For ($I=1; $ I<= 3; $ I ++) // three times for a total of four
  59. {
  60. For ($J=1; $ J<= 4; $ j ++) // four people
  61. {
  62. For ($K=1; $ K<= 4; $ k ++) // Four at a time
  63. {
  64. $Name='User'. $ J;
  65. $ {$ Name} [] = $ majiangArray [$ seek ++];
  66. }
  67. }
  68. }
  69. // Then one person
  70. For ($I=1; $ I<= 4; $ I ++)
  71. {
  72. $Name='User'. $ I;
  73. $ {$ Name} [] = $ majiangArray [$ seek ++];
  74. }
  75. // The Banker finally finds one more
  76. $ User4 [] = $ majiangArray [$ seek];
  77. // The result of the sub-card ending Display
  78. ShowArray ($ user1 );
  79. ShowArray ($ user2 );
  80. ShowArray ($ user3 );
  81. ShowArray ($ user4 );
  82. ?>

Bytes. PHP array is the most common batch data storage...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.