eits Posted November 11, 2008 Share Posted November 11, 2008 I'm trying to write the backend to a card game in PHP. However, I am at the moment stumped! All I need to do is check if a value is in an array and if it is, return true. Here is my code so far: function checkcard($newcard) { $dealtcards = file("file.crd"); if (in_array($newcard, $dealtcards)) { return true; } } The file.crd is just a file with a new value on each line. e.g. 10S is 10 of spades. Can somebody shed some light on this please? Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/ Share on other sites More sharing options...
bobbinsbro Posted November 11, 2008 Share Posted November 11, 2008 i think the newline char on the end of each line in the file is causing the mis-match. file() does not cut the newline off the end of each line. Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687783 Share on other sites More sharing options...
F1Fan Posted November 11, 2008 Share Posted November 11, 2008 First, please put your code in the tags. Second, are you getting any errors? What is this code doing? Also, the in_array() function returns true or false, so you could just return the in_array() function itself. Is that your whole function? Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687785 Share on other sites More sharing options...
eits Posted November 11, 2008 Author Share Posted November 11, 2008 OK. I'm not getting any errors. I should be able to pass 'AA' to the function and it'll check that file to see if 'AS' (or any given card) is already in the file and if it is, return a value (true / false) so that I can choose a different card otherwise two cards will be dealt. I have chopped this function up at least ten times today so yea now that is all I have. If I print_r after the $dealtcards = file.... line I get: Array ( [0] => 3S [1] => 7H [2] => AD [3] => 10S [4] => 9D [5] => 10S [6] => 5D [7] => 9C [8] => 6S [9] => 3C ) Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687787 Share on other sites More sharing options...
Maq Posted November 11, 2008 Share Posted November 11, 2008 I think bobbinsbro is right, you need to add this context parameter in the file function. Try this: function checkcard($newcard) { $dealtcards = file("file.crd", FILE_IGNORE_NEW_LINES); if (in_array($newcard, $dealtcards)) { return true; } } ***Warning, I think FILE_IGNORE_NEW_LINES only work with PHP 5.0+. Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687791 Share on other sites More sharing options...
eits Posted November 11, 2008 Author Share Posted November 11, 2008 FILE_IGNORE_NEW_LINES didn't seem to work I copied and pasted that code and modified return true to echo "works". But no sign of that magical word. :-( Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687796 Share on other sites More sharing options...
bobbinsbro Posted November 11, 2008 Share Posted November 11, 2008 you could try explicitly trimming off the newlines: function checkcard($newcard) { $dealtcards = file("file.crd"); foreach ($dealtcards as $line){ trim($line); } if (in_array($newcard, $dealtcards)) { return true; } } Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687799 Share on other sites More sharing options...
wildteen88 Posted November 11, 2008 Share Posted November 11, 2008 you could try explicitly trimming off the newlines: function checkcard($newcard) { $dealtcards = file("file.crd"); foreach ($dealtcards as $line){ trim($line); } if (in_array($newcard, $dealtcards)) { return true; } } That wont work! Use function checkcard($newcard) { $dealtcards = file("file.crd"); $dealtcards = array_map('trim', $dealtcards); // remove the new lines. if (in_array($newcard, $dealtcards)) { return true; } } Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687926 Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 Or simply: $dealtcards = file("file.crd", FILE_IGNORE_NEW_LINES); =/ Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687929 Share on other sites More sharing options...
wildteen88 Posted November 11, 2008 Share Posted November 11, 2008 Or simply: $dealtcards = file("file.crd", FILE_IGNORE_NEW_LINES); =/ Already been suggested and didn't work http://www.phpfreaks.com/forums/index.php/topic,225111.msg1037215.html#msg1037215 Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687930 Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 Or simply: $dealtcards = file("file.crd", FILE_IGNORE_NEW_LINES); =/ Already been suggested and didn't work http://www.phpfreaks.com/forums/index.php/topic,225111.msg1037215.html#msg1037215 Ah, didn't see that. It works on PHP5. @_@ array_map(), as wildteen suggested, is your best bet other than upgrading your PHP, which would be the best solution. Quote Link to comment https://forums.phpfreaks.com/topic/132291-this-is-really-annoying-me-now-php-array-from-file-please-help/#findComment-687931 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.