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
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
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
Share on other sites

change

 

while($Tee = mysql_fetch_array($Query)) {
      $Comments .= $Tee['Comment'];
   }

 

to

 

while($Tee = mysql_fetch_array($Query)) {
      $Comments[] = $Tee['Comment'];
   }

 

 

 

Link to comment
Share on other sites

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.