使用in_array/php卡经销商程序

使用in_array/php卡经销商程序,php,Php,这把我难住了。基本上我是在试着发牌。我得到了正确数量的卡片,但它复制了随机生成的卡片。相反,如果用户输入52,我需要有52张不同的卡 我一直在努力做这件事,但我想不出来。我尝试使用in_数组函数,但没有效果 有人能帮忙吗 <?php $input = $_POST["txtNumberOfCards"]; $suit = array("Hearts", "Diamonds", "Clubs", "Spades"); $card = array("Ace","2", "3","4","5"

这把我难住了。基本上我是在试着发牌。我得到了正确数量的卡片,但它复制了随机生成的卡片。相反,如果用户输入52,我需要有52张不同的卡

我一直在努力做这件事,但我想不出来。我尝试使用in_数组函数,但没有效果

有人能帮忙吗

<?php
$input = $_POST["txtNumberOfCards"];

$suit = array("Hearts", "Diamonds", "Clubs", "Spades");
$card = array("Ace","2", "3","4","5","6","7","8","9","10","Jack","Queen","King");

$randsuit = rand(0,3);
$randcard = rand(0,12);

for($x = 0; $x < $input; $x++){
echo ('<img src="Images/'.$card[$randcard].'of'.$suit[$randsuit].'.gif">');
}

?>

您需要使用函数
intval()

此外,每次循环时随机分配varilabel以生成不同的卡:

<?php
$input = intval($_POST["txtNumberOfCards"]);

$suit = array("Hearts", "Diamonds", "Clubs", "Spades");
$card = array("Ace","2", "3","4","5","6","7","8","9","10","Jack","Queen","King");


for($x = 0; $x < $input; $x++){
$randsuit = rand(0,3);
$randcard = rand(0,12);
echo ('<img src="Images/'.$card[$randcard].'of'.$suit[$randsuit].'.gif">');
}

?>

使用数组跟踪已使用的卡,并采取相应的行动。在代码中,for循环之外还有rand函数,这意味着它只会生成一次随机数

$input = $_POST["txtNumberOfCards"];;

$suit = array("Hearts", "Diamonds", "Clubs", "Spades");
$card = array("Ace","2", "3","4","5","6","7","8","9","10","Jack","Queen","King");

$setCards = array();
for($x = 0; $x < $input; $x++){
    $randsuit = rand(0,3);
    $randcard = rand(0,12); 
    if( isset($setCards[$suit[$randsuit].$card[$randcard]]) ) {
        $x--;
        continue;
    }
    echo ('<img src="Images/'.$card[$randcard].'of'.$suit[$randsuit].'.gif">');
    $setCards[$suit[$randsuit].$card[$randcard]] = true;
}
$input=$\u POST[“txtNumberOfCards”];;
$suit=阵列(“红心”、“钻石”、“梅花”、“黑桃”);
$card=阵列(“Ace”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“杰克”、“女王”、“国王”);
$setCards=array();
对于($x=0;$x<$input;$x++){
$randsuit=兰特(0,3);
$randcard=兰特(0,12);
如果(isset($setCards[$suit[$randsuit]。$card[$randcard]])){
$x——;
继续;
}
回声(“”);
$setCards[$suit[$randsuit]。$card[$randcard]]=true;
}

:注意,fiddle会回显html,以便可以查看而不是呈现。

您获得重复卡片的原因是您没有检查卡片组合是否已被处理

最好的解决方案是建立一个卡片阵列,并使用它来检查是否创建了卡片组合。然后回显构建的阵列

<?php
//The user input
$input = $_POST["txtNumberOfCards"];

$suit = array( "Hearts" , "Diamonds" , "Clubs" , "Spades" );
$card = array( "Ace" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King");
//$dealArray holds the cards as they are dealt and used to compare what cards have all ready been dealt
$dealArray= array( );
//$count the while loop counter variable
$count = 0;
//while the $count variable is less then the $input
while( $count < $input )
{
  //generate a card combination array
  $cardCombo = array( 'suit' => $suit[ rand( 0 , 3 ) ],//Generate Random suit
                      'card' => $card[ rand( 0 , 12 ) ] );//Generate Random card

  //if the $cardCombo array that was generated is not in the $dealArray
  if ( !in_array( $cardCombo , $dealArray ) )
  {
    //Add the $cardCombo array to the end of the $dealArray
    array_push( $dealArray , $cardCombo );
    //Output an HTML image for the $cardCombo array
    echo ( '<img src="Images/' . $cardCombo['card'] . 'of' . $cardCombo['suit'] . '.gif">' );
    $count++;//Add 1 to the counter
  }
}
?>


这是因为您没有检查卡片是否已经生成。我没有否决投票,但这不会检测到重复项。@neeagl-问题是为什么他的代码返回重复项。@RichardChristensen这主要是因为他没有更新
$randsuit
$randcard
值。对于每次迭代,它们总是相同的值。然而,Patrick正确地指出,我没有检查我同意的重复项。另外,您也不“需要”intval(尽管它有利于卫生),因为php使用类型强制,所以在需要数字的地方使用字符串时,字符串将被强制为数字。好吧,我不知道这一点。ThanksHey thanks唯一的问题是,当我使用我的用户输入时,它不会返回正确的卡片数…我输入了5张,得到了3张卡片。我将其更改为while循环,以便只有在没有匹配的情况下才会计数,而不是无论函数结果如何都计数的for循环。谢谢你介意为我评论代码吗所以,因为其中一些对我来说仍然没有意义。例如,卡片组合不知道这意味着什么,并提前表示感谢。我对代码进行了一些编辑,以实现@Patrick Evans在创建卡片时对其进行回音的使用,而不必在构建之后在阵列中循环。非常感谢,我真的非常感谢。这对我来说确实比foreachI更合理,就像你如何将卡的生成检查和显示组合在一个for循环中一样,但是我总是使用带有负逻辑的while循环来检查,这样如果存在重复,我就不必“撤消”计数器。