Jump to content

Array Trouble


N-Bomb(Nerd)

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.