31 users online (1 members and 30 guests)  

Thread: array problems


  Results 1 to 2 of 2

Related

  1. Replies: 4
  2. Array to string    Forum: PHP Forum
    Replies: 3
  3. Getting min/max values from an array    Forum: CGI Perl Forum
    Replies: 1
  4. Dynamic JS Array Problems in IE6    Forum: Javascript Forum
    Replies: 0
  5. fetch array problems    Forum: PHP Forum
    Replies: 4
  1. #1
    Zoup's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2007
    Posts
    8

    array problems

    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. #2
    Zoup's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2007
    Posts
    8

    Re: array problems

    figured it out

    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);
    ?>



Tags for this Thread