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
    Nov 2011
    Location
    Notre Dame, IN
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Print no duplicates

    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.

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    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))

  • Users who have thanked Adee for this post:

    SuperMode55 (11-21-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    Notre Dame, IN
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by SuperMode55 View Post
    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.

  • Users who have thanked Adee for this post:

    SuperMode55 (11-21-2011)

  • #5
    New Coder
    Join Date
    Nov 2011
    Location
    Notre Dame, IN
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Yeah it makes sense. i appreciate the help.


  •  

    Posting Permissions

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