本文介紹情景為活動人選抽獎,通過簡單隨機抽取指定人數(shù),依次列舉被抽中的人名。示例代碼如下:
<!DOCTYPE html><html>  <head>    <meta charset="UTF-8">    <title>活動人選</title>  </head>  <body>  <?php  $lotterPeopleArr = array();  $num = 8;  for($i=0;$i<$num;$i++){    $lotterPeople = GetRandStr(1);    while(in_array($lotterPeople, $lotterPeopleArr)){      $lotterPeople = GetRandStr(1);    }    $lotterPeopleArr[$i] = $lotterPeople;    echo "抽中人:". $lotterPeopleArr[$i].'<br/>';  }  // echo '抽中人:'. $lotterPeople;  /**   * 生成隨機數(shù)   * $type int 默認(字母&&數(shù)字) 1 純字母 2.純數(shù)字 3.英文單詞   */   function GetRandStr($len='4',$type='3')  {    switch($type)    {      case '1';      $chars = array(          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",          "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",          "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",          "S", "T", "U", "V", "W", "X", "Y", "Z"      );      break;      case '2';      $chars = array(          "0", "1", "2",          "3", "4", "5", "6", "7", "8", "9"      );      break;      case '3';      $chars = array(          "小華","小冠","小王","小吳","小朱","小何","小陳","小熊","小斌","小李","小春","小超","小張","小郭","小柯","小梁","小理","小龔","小杰","小余","小洪","小鄭","小志","小潘",      );      break;      default;      $chars = array(          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",          "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",          "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",          "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",          "3", "4", "5", "6", "7", "8", "9"      );      break;    }    $charsLen = count($chars) - 1;    shuffle($chars);    $output = "";    for ($i=0; $i<$len; $i++)    {    $output .= $chars[mt_rand(0, $charsLen)];    }    return $output;  }  ?>  </body></html>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點
疑難解答
圖片精選