Newb lost in a nested loop
Posted: Mon May 11, 2009 11:06 pm
I'm starting out learning PHP and find myself stuck in a function to create a deck of cards.
What I want to end up with is an associative array with a suit/card key like Queen4 or Q4 and a numeric value for the card, so Q4=>4, K8=>8 and so on. I should add that aces are 1 and Jack, Queen, King are 11,12 and 13.
I need to do it for every suit, and I'm attempting to do it with nested foreach loops.
Here's what I have:
=============================================================
======================================================
This, unfortunately, only puts the last value of $var3 in the array which otherwise works.
Can someone help me out?
What I want to end up with is an associative array with a suit/card key like Queen4 or Q4 and a numeric value for the card, so Q4=>4, K8=>8 and so on. I should add that aces are 1 and Jack, Queen, King are 11,12 and 13.
I need to do it for every suit, and I'm attempting to do it with nested foreach loops.
Here's what I have:
=============================================================
Code: Select all
function getDeck(){
$deck = array();
$card = range(1,13);
$suit = array(C,D,H,S);
$value = array(1,2,3,4,5,6,7,8,9,10,10,10,10);
foreach($suit as $var1){
foreach($card as $var2){
foreach($value as $var3){
$deck[$var1.$var2] = $var3;
}
}
}
print_r ($deck);
}
This, unfortunately, only puts the last value of $var3 in the array which otherwise works.
Can someone help me out?