N-Bomb(Nerd) Posted April 7, 2009 Share Posted April 7, 2009 I seem to be having some problems with in_array().. I know for a fact that my variable is in the array because right now I'm just testing using $_GET.. that way I can test this for sure. However, when I try to do the in_array() using the same exact word it always turns up false.. have a look real quick: // selects all of the user comments $Query = mysql_query("SELECT `Comment` FROM `test` WHERE `Name` = '" . $Name . "'"); $Comments = array(); while($Tee = mysql_fetch_array($Query)) { $Comments .= $Tee['Comment']; } echo 'New Comment: ' . $Comment_New . '<br />'; echo 'All User Comments - <br />'; print_r($Comments); echo '<br /><br />'; if (in_array($Comment_New, $Comments)) { echo 'it worked!'; } else { echo 'failed..'; } When this is ran it always outputs "failed..".. even when $Comment_New is in the array $Comments.. any idea as to why this is? If this is confusing let me know and I'll do my best to try and explain it again.. Quote Link to comment Share on other sites More sharing options...
phil88 Posted April 7, 2009 Share Posted April 7, 2009 What is the exact output of the print_r($Comments) statement? Also, what exactly does $Comments have in it? Quote Link to comment Share on other sites More sharing options...
N-Bomb(Nerd) Posted April 7, 2009 Author Share Posted April 7, 2009 I'm only using simple one word comments to make the testing easier at the moment, but when I did this last the output was: New Comment: crazy Arrayniceamazing failed.. So the words "nice" and "amazing" is in $Comments, because $Comments is the array that holds all of the user comments from the database. In this case "$Comment_New" was "crazy".. so really when doing the in_array() it would be like this in theory: if (in_array("crazy", $Comments)) { echo 'it worked!'; } else { echo 'failed..'; } it will return 'failed..' Which in this case is true.. but when I change $Comment_New to "nice".. it will still return "failed.." even though its in the array Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 7, 2009 Share Posted April 7, 2009 that's because $Comments is not an array, so checking values against like it is is going to fail every time. first, always use : is_array() to make sure what you're dealing with is actually an array or not .. save you lots'o'time. $Comments is not actually an array 'cause you say it is : $Comments = array(); it needs to be populated like an array. really, you should take a better look into what an array is. $Comments = array ("crazy","stupid","fun","smelly","happy"); if (is_array($Comments)) { if (in_array("crazy", $Comments)) { echo "It's there"; } else { echo "Not there"; } } else { echo "Not even an array .. geez, man."; } //will output: It's there Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 7, 2009 Share Posted April 7, 2009 change while($Tee = mysql_fetch_array($Query)) { $Comments .= $Tee['Comment']; } to while($Tee = mysql_fetch_array($Query)) { $Comments[] = $Tee['Comment']; } Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 7, 2009 Share Posted April 7, 2009 ^what he said .. that will fill your array with your comments, then your can do whatever you want with $Comments as it will then be an array (assuming it gets populated with values from the db) Quote Link to comment 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.