j4ymf Posted August 2, 2009 Share Posted August 2, 2009 hello all im trying to get an array to work but i just cant do it. my aim is, im running a fantasy football site and i don't want people to be able to add the same player in twice. how would i go about doing it? this is what i have and it dosen't work many thanks j4ymf function check_player_ids ($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11) { //Put the players into an array $chosen_players = array($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11); //Remove duplicates $unique_players = array_unique($chosen_players); //Compare the before and after to get any duplicates out $duplicate_players = array_diff($chosen_players, $unique_players); //Now check if there are any duplicates if (count($duplicate_players) > 0) { global $bad_player; $bad_player = $duplicate_players[0]; // only returns the first duplicate, but could be modified to return then all return False; } else { return True; } } Quote Link to comment https://forums.phpfreaks.com/topic/168540-solved-cant-get-this-array-to-work/ Share on other sites More sharing options...
wildteen88 Posted August 2, 2009 Share Posted August 2, 2009 Define what you mean by "it doesnt work". What is you function supposed to do? What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/168540-solved-cant-get-this-array-to-work/#findComment-889067 Share on other sites More sharing options...
j4ymf Posted August 2, 2009 Author Share Posted August 2, 2009 Sorry i will try to explain well im trying to check if the team has any duplicates, so i have 11 players being entered, but at the moment it lets the team through when you are entering it, even if i have two players the same. so im trying to avoid terry being put in twice!!! does that make sence so im after the code to check if it has any duplicates in it, thanks j4ymf jason Quote Link to comment https://forums.phpfreaks.com/topic/168540-solved-cant-get-this-array-to-work/#findComment-889073 Share on other sites More sharing options...
Mark Baker Posted August 2, 2009 Share Posted August 2, 2009 function check_player_ids ($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11) { //Put the players into an array $chosen_players = array($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11); //Remove duplicates $unique_players = array_unique($chosen_players); //Compare the before and after to get any duplicates out $duplicate_players = array_diff_assoc($chosen_players, $unique_players); //Now check if there are any duplicates if (count($duplicate_players) > 0) { global $bad_player; $bad_player = array_shift($duplicate_players); // only returns the first duplicate, but could be modified to return then all return False; } else { return True; } } Quote Link to comment https://forums.phpfreaks.com/topic/168540-solved-cant-get-this-array-to-work/#findComment-889105 Share on other sites More sharing options...
j4ymf Posted August 2, 2009 Author Share Posted August 2, 2009 thankyou mark you are a star, just what i neeeded Quote Link to comment https://forums.phpfreaks.com/topic/168540-solved-cant-get-this-array-to-work/#findComment-889129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.