Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multidimensional array question

    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.

    PHP Code:
    $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

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Gotcha on the unset but how exactly to I pick a random out of this array and have it output Clubs - Ace? Thanks

    PHP Code:
    $cards = array('Clubs' => array('Ace' => 1,'King' => 2,'Queen' => 3));
                                
    echo 
    $random_key array_rand($cards1); 

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    figured it out.

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

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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($cards1);
    $random_card array_rand($cards[$random_suite],1);
    $random_player array_rand($players1);
        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);
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •