0
我對編程完全陌生,至少在幾個小時內一直在努力。它只需要5張牌給玩家。這是我的代碼:如何將for循環的值存儲在數組中?
<?php
//setting up arrays
$cardLocation = array();
$suits = array("Hearts", "Diamonds", "Spades", "Clubs");
$ranks = array("Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack","Queen", "King");
//filling the deck
for($rank=0; $rank<13; $rank++){
for($suit=0; $suit<4; $suit++){
$cardLocation[$rank][$suit] = "deck";
}
}
//dealing the cards to a player
for($i=0; $i<5; $i++){
$duplicate = true;
while($duplicate){
$suit = rand(1, 4);
$rank = rand(1, 13);
if($cardLocation[$rank][$suit] == "deck"){
$cardLocation[$rank][$suit] = "player";
$duplicate = false;
}
}
}
?>
我試圖找出的for循環的每個值存儲到一個數組,然後打印出來的方法。有幾個想法,但都失敗了。任何幫助將受到歡迎。
有點offtopic:我建議檢查一個'foreach'循環。基本上,它會爲數組的每個值運行一個循環,因此在稍後更改數組時,您不必限制長度。另外寫起來更簡單,更短。嘗試'foreach($作爲$ value)'' – switz