PHP重新排列陣列中的元素shuffle()函數
Temperature: 0 °C
shuffle() 是可以將陣列中元素的順序重新洗牌的函數。
甚麼時候用的到?
例如最近寫著一個抽獎機制,共有10組得獎這邊先設定為YES,10組未得獎的元素這邊先設定為NO。
而他在陣列中排列的方式為:
$array = array("YES","YES","YES","YES","YES","YES","YES","YES","YES","YES","NO","NO","NO","NO","NO","NO","NO","NO","NO","NO");
當使用shuffle()重新排列時,將會隨機呈現如下:
$array = array("YES","NO","YES","NO","NO","YES","YES","YES","NO","YES","NO","YES","NO","NO","YES","NO","YES","NO","YES","NO");
再來搭配機率來取得中獎。
以上紀錄~
#故事 #PHP #shuffle #重新洗牌