...

View Full Version : multidimensional array question



rschoenbach
04-04-2007, 05:56 AM
I am trying to build a random card generator and once a card is chosen it gets taken out of the array.

this is what i have so far which is far away from where I need to be.



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


Any suggestions on how I would go about using the array_rand command to pull out and then delete the record out of the array?

Ideally I would like to use array_push to output the results to another array.

Thanks for the help

iLLin
04-04-2007, 06:09 AM
unset() (http://us.php.net/manual/en/function.unset.php)

rschoenbach
04-04-2007, 06:20 AM
Gotcha on the unset but how exactly to I pick a random out of this array and have it output Clubs - Ace? Thanks


$cards = array('Clubs' => array('Ace' => 1,'King' => 2,'Queen' => 3));

echo $random_key = array_rand($cards, 1);

rschoenbach
04-04-2007, 06:27 AM
figured it out.


$cards = array('Clubs' => array("Ace" => 1,"King" => 2,"Queen" => 3));
$random_key = array_rand($cards, 1);
$random_key1 = array_rand($cards[$random_key],1);

rschoenbach
04-04-2007, 08:00 AM
I have made good progress tonight on this program and my eyes hurt.

Please run this code and help me figure out howto parse out the results in this array.

I am confused.

Thanks


<?php
$ryan = array();
$ronald = array();
$ginger = array();
$brent = array();

function remove_element_card (&$array, $suite, $card) // pass array by reference
{
unset($array[$suite][$card]);
}
function remove_element(&$array, $suite)
{
unset($array[$suite]);
}

$cards = array('Clubs' => array("Ace" => 1,"King" => 2,"Queen" => 3, "Jack" => 4, "Ten" => 5, "Nine" => 6),
'Hearts' => array("Ace" => 1,"King" => 2,"Queen" => 3, "Jack" => 4, "Ten" => 5, "Nine" => 6),
'Diamonds' => array("Ace" => 1,"King" => 2,"Queen" => 3, "Jack" => 4, "Ten" => 5, "Nine" => 6),
'Spades' => array("Ace" => 1,"King" => 2,"Queen" => 3, "Jack" => 4, "Ten" => 5, "Nine" => 6),);

$players = array('Ryan' => 1, 'Brent' => 2, 'Ronald' => 3, 'Ginger' => 4);

for ($y = 1; $y<=20; $y++) {
$random_suite = array_rand($cards, 1);
$random_card = array_rand($cards[$random_suite],1);
$random_player = array_rand($players, 1);
switch ($random_player) {
case "Ryan":
$e++;
array_push($ryan, $random_card, array($random_suite));
if ($e == 5) {
remove_element($players, $random_player);
}
break;
case "Brent":
$f++;
array_push($brent, $random_card);
if ($f == 5) {
remove_element($players, $random_player);
}
//echo $random_card.' of '.$random_suite.' for '.$random_player.'<br />';
break;
case "Ronald":
$g++;
array_push($ronald, $random_card);
if ($g == 5) {
remove_element($players, $random_player);
}
//echo $random_card.' of '.$random_suite.' for '.$random_player.'<br />';
break;
case "Ginger":
$h++;
array_push($ginger, $random_card);
if ($h == 5) {
remove_element($players, $random_player);
}
//echo $random_card.' of '.$random_suite.' for '.$random_player.'<br />';
break;
}

switch ($random_suite) {
case "Clubs":
$a++;
//echo $random_card . ' of ' . $random_suite . '<br />';
remove_element_card($cards, $random_suite, $random_card);
if ($a == 6) {
//echo '<br /><strong>removing '.$random_suite . '</strong><br /><br />';
remove_element($cards, $random_suite);
}
break;
case "Diamonds":
$b++;
//echo $random_card . ' of ' . $random_suite . '<br />';
remove_element_card($cards, $random_suite, $random_card);
if ($b == 6) {
//echo '<br /><strong>removing '.$random_suite . '</strong><br /><br />';
remove_element($cards, $random_suite);
}
break;
case "Spades":
$c++;
//echo $random_card . ' of ' . $random_suite . '<br />';
remove_element_card($cards, $random_suite, $random_card);
if ($c == 6) {
//echo '<br /><strong>removing '.$random_suite . '</strong><br /><br />';
remove_element($cards, $random_suite);
}
break;
case "Hearts":
$d++;
//echo $random_card . ' of ' . $random_suite . '<br />';
remove_element_card($cards, $random_suite, $random_card);
if ($d == 6) {
//echo '<br /><strong>removing '.$random_suite . '</strong><br /><br />';
remove_element($cards, $random_suite);
}
break;
}
}
print_r($ryan);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum