简体   繁体   中英

php array trouble

I'm a rookie with php arrays, and have a problem. I downloaded a blackjack PHP script, it stores the current players hand, deck, and dealers hand in THE $_POST , which isn't good.

So I'm trying to alter it to store them in a database instead. I'm getting errors and this is the code I'm playing with. The original code for drawing a random card from the deck is this:

    shuffle($deck); 

    for ($i = 0; $i < 2; $i++) {
       $hand[] = array_shift($deck);
       $dealer[] = array_shift($deck);
       }

    $handstr = serialize($hand);
    $deckstr= serialize($deck);
    $dealerstr= serialize($dealer);

This works, but what I want to do is only draw a random card if theres no data in the database already. If the user draws, someone could just refresh the page to get a different hand. I want to do something like this:

if ($rs5[hand] == "") {

  shuffle($deck); 

  for ($i = 0; $i < 2; $i++) {
    $hand[] = array_shift($deck);
    $dealer[] = array_shift($deck);
    }

  $handstr = serialize($hand);
  $deckstr= serialize($deck);
  $dealerstr= serialize($dealer);

} else {

   $dealer = $rs5[dealer];
   $hand = $rs5[hand];
   $deck = $rs5[deck];


}

Im getting errors with this, I don't know what I'm doing with arrays really, can anyone point me in the right direction?

I'm not terribly sure what you're trying to do, but for starters:

$dealer = $rs5[dealer];
$hand = $rs5[hand];
$deck = $rs5[deck];

Should probably be:

$dealer = $rs5[$dealer];
$hand = $rs5[$hand];
$deck = $rs5[$deck];

Note the dollar signs on the index variables.

You could do:

if (empty($hand))

or:

if (count($hand) == 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM