in_array


#1

Hi there,

I want to check if a check if a certain value is in array with a row from my database, I think it is something like this but this code doesn’t give the right result, the value is never in array:


$sql = mysql_query("SELECT Row FROM table WHERE Type='Type'");
while ($row = mysql_fetch_array($sql)) {

$array = array("'".$row['Row']."', ");
}
if (in_array("Value", $array, true)) {
    // positive
}
else
{
   // negative
}

Hope you guys know what’s wrong about it.
Thanks


#2

Have you tried in_array without the third parameter?


#3

Well the thing is that if I print

"'".$row['Row']."', "

and place that manually in the array the whole script does actually work which seems very weird to me.

Francis


#4

I think that your problem may lie in the quotes. I see you have about 4 sets within the same array, making it very confusing just to look at. I would suggest escaping the quotes that are inside.

This mearly is just a suggestion though, being that I’m a newbie. :P
Hope you work things out!

-Teh Riddler


#5

Ah, now I see the problem: you’re overwriting $array with a new, clean array each time you loop through the result resource. Try something like this:

[php]
$array = array();
$i = 0;
while ($row = mysql_fetch_array($sql)) {
$array[$i++] = $row[‘Row’];
}
[/php]

Then it should work.


#6

@Zyppora

It works fine, much appreciated :D .

Francis