Jump to content

This is really annoying me now! PHP array from file PLEASE HELP!


Recommended Posts

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?

 

 

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?

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?

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+.

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; }
}

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; }
}

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.