SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A simple Array, Loop Question :-)

    Hey!

    I want to make my array work like this:

    PHP Code:
    $cardArry = array('5'=>'C''4'=>'D''11'=>'H''12'=>'C''3'=>'D'); 
    Ok.

    So I have a loop but can't figure out how to make it that way, This one doesn't work, I tried a few other ways below but couldnt get it to print right.

    PHP Code:
    for ($i 0$i 5$i++){

        
    // Generate Random Card
        
    $randCard rand(2,14);
        
    $randSuit rand(0,3);

        
    $cardArr[] = array("$randCard"=>"$suit");
    }

    print_r($cardArr); 

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    for ($i 0$i 5$i++){

        
    // Generate Random Card
        
    $randCard rand(2,14);
        
    $randSuit rand(0,3);

        
    $cardArr[$randCard] = $randSuit;
    }

    print_r($cardArr); 

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey that works, I just have to prevent it from doing the same card twice

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't have to it will overwrite the old one.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But when override occurs, $i is increased anyway, so he gets shorter array. Try this:

    PHP Code:
    while (count($cardArr) < 5)
    {
        
    // Generate Random Card
        
    $randCard rand(2,14);
        
    $randSuit rand(0,3);
        
        
    $cardArr[$randCard] = $randSuit;
    }

    print_r($cardArr); 


Bookmarks

Posting Permissions

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