# Thread: A simple Array, Loop Question :-)

1. ## 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. 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. Hey that works, I just have to prevent it from doing the same card twice

4. You don't have to it will overwrite the old one.

5. 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);  ```

#### Posting Permissions

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