...

View Full Version : Print no duplicates



SuperMode55
11-21-2011, 02:15 AM
Hello I am fairly new to PHP so go easy on me. I am making a game displaying 5 cards in the browser. I am using an array to store my cards. Then I tell it to echo a random array and in the same time I store that result in a variable called $card1 through $card5, problem is that I get the same cards sometimes. What would be the best way to get rid of this issue.
The way I thought about doing it was to echo $card1 and then echo $card2 except cannot be equal to $card1, I used the != operator witch didn't work, just need help with what can be used in a statement where you echo something out of a list with the exception of something else of that list so i dont have duplicates.Thanks.

Adee
11-21-2011, 02:17 AM
just store the cards in an array like $cards = array('card1'=>whatever .. etc

then when you're adding them to the array use if(!in_array($card, $cards))

SuperMode55
11-21-2011, 02:28 AM
Thanks for getting me going, looked at that and then pasted it into w3 and it came up
Definition and Usage

The in_array() function searches an array for a specific value.

This function returns TRUE if the value is found in the array, or FALSE otherwise.
Thank you.

Adee
11-21-2011, 02:31 AM
Thanks for getting me going, looked at that and then pasted it into w3 and it came up
Definition and Usage

The in_array() function searches an array for a specific value.

This function returns TRUE if the value is found in the array, or FALSE otherwise.
Thank you.


no problem. so, you're adding cards to your 5 card array, and if the card it is about to add while going through your array of all cards is already in the 5 card array, it will skip it and go to the next card until it finds one that isn't already there, and then add it.

SuperMode55
11-21-2011, 02:36 AM
Yeah it makes sense. i appreciate the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum