Jump to content

[SOLVED] session problem


phpcodec

Recommended Posts

I have a session issue

 

$types = array("diamonds","hearts","spades","clubs");
$cards = array("Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King");
$possible_cards = array();
foreach($types AS $type)
{
foreach($cards AS $card)
{
$possible_cards[] = strtolower($card).':of:'.$type;
}
}

 

I have this code to generate card names such as "8:of:diamonds" and such, this works fine

 

this is the print_r result of $possible_cards array

 

Array ( [0] => ace:of:diamonds [1] => 2:of:diamonds [2] => 3:of:diamonds [3] => 4:of:diamonds [4] => 5:of:diamonds [5] => 6:of:diamonds [6] => 7:of:diamonds [7] => 8:of:diamonds [8] => 9:of:diamonds [9] => 10:of:diamonds [10] => jack:of:diamonds [11] => queen:of:diamonds [12] => king:of:diamonds [13] => ace:of:hearts [14] => 2:of:hearts [15] => 3:of:hearts [16] => 4:of:hearts [17] => 5:of:hearts [18] => 6:of:hearts [19] => 7:of:hearts [20] => 8:of:hearts [21] => 9:of:hearts [22] => 10:of:hearts [23] => jack:of:hearts [24] => queen:of:hearts [25] => king:of:hearts [26] => ace:of:spades [27] => 2:of:spades [28] => 3:of:spades [29] => 4:of:spades [30] => 5:of:spades [31] => 6:of:spades [32] => 7:of:spades [33] => 8:of:spades [34] => 9:of:spades [35] => 10:of:spades [36] => jack:of:spades [37] => queen:of:spades [38] => king:of:spades [39] => ace:of:clubs [40] => 2:of:clubs [41] => 3:of:clubs [42] => 4:of:clubs [43] => 5:of:clubs [44] => 6:of:clubs [45] => 7:of:clubs [46] => 8:of:clubs [47] => 9:of:clubs [48] => 10:of:clubs [49] => jack:of:clubs [50] => queen:of:clubs [51] => king:of:clubs )

 

 

I make a loop statement to add random card names into sessions

 

for($i=1;$i<=6;$i++)
{
$rand = rand(0,sizeof($possible_cards));
$_SESSION['blackjack_your_card_'.$i] = $possible_cards[$rand];
unset($possible_cards[$rand]);

$rand = rand(0,sizeof($possible_cards));
$_SESSION['blackjack_dealer_card_'.$i] = $possible_cards[$rand];
unset($possible_cards[$rand]);
}

 

this is where the problem lies, some of the session variables such as $_SESSION['blackjack_dealer_card_3'] will be filled in as blank instead of "9:of:clubs" or whatever

 

Could anyone help me find the source of this problem?

Link to comment
https://forums.phpfreaks.com/topic/132015-solved-session-problem/
Share on other sites

Okay I think your problem is this: you are unsetting elements in your array but you are not re-indexing your array, so when you turn around and pick a random element, you could randomly be picking elements that no longer exist.  Example:

 

<?php
   $array = array('a','b','c');          // example array
   print_r($array);                      // output: Array ( [0] => a [1] => b [2] => c ) 
   echo "<br/>";
   unset($array[1]);                   // remove random element
   print_r($array);                      // output: Array ( [0] => a [2] => c ) 
   echo "<br/>";
   // at this point in time, you have 2 elements. If you do this...
   $x = rand(0,1);
   echo $array[$x];                    
   // you have 2 possible outputs: 'a' or nothing, because
   // $array[0] exists, but $array[1] does not

   // so what you want to do is this:   
   $array = array_values($array); // re-index the array keys
   print_r($array);                     // output: Array ( [0] => a [1] => c ) 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.