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.. Link to comment https://forums.phpfreaks.com/topic/153039-array-trouble/ 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? Link to comment https://forums.phpfreaks.com/topic/153039-array-trouble/#findComment-803771 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 Link to comment https://forums.phpfreaks.com/topic/153039-array-trouble/#findComment-803779 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 Link to comment https://forums.phpfreaks.com/topic/153039-array-trouble/#findComment-803801 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']; } Link to comment https://forums.phpfreaks.com/topic/153039-array-trouble/#findComment-803812 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) Link to comment https://forums.phpfreaks.com/topic/153039-array-trouble/#findComment-803819 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.