Jeff Solomon
Jeff Solomon

Reputation: 473

Array not containing values when using "in_array" (php) w/ mysql_query

I continue to struggle with array! This is probably easy to answer.

I'm retrieving a data set from MYSQL w/ PHP. I get an array that has the 1st row (ala the mysql_fetch_array). Typically I would just loop through this and get each value, but in this case I'm already in the middle of a loop and I need to find out if a particular value exists in the full data set (which will be more than 1 row).

I figured I could just loop through and put all the values into an array with something like:

$query = "SELECT MapId FROM Map Where GameId = $gl_game_id";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);     
$map_set = array();
while($row = mysql_fetch_assoc($result_set)) {
    $map_set[] = $row;
}   

When I print_r this, I do in fact get the full data set (e.g. all rows of MapId from table Map).

So now, when I go to look in there and see if a value (that is coming out of this other loop) exists, it won't find it.

So my code looks like:

if (in_array($i, $map_set)) {
    echo "yes, it's there baby!";       
}

But it doesn't work. I tried hard coding the array, and that does in fact work. So there is simply something wrong with the way I'm constructing my array that this function doesn't like it.

if (in_array($i, array(40,12,53,65))) {
    echo "yes, it's there baby!";       
}       

arrrg... I do hate being a noobie at this.

Upvotes: 1

Views: 909

Answers (1)

newman
newman

Reputation: 2719

Function mysql_fetch_assoc returned array.

If you make print_r($map_set) then you will see that is 2-dimension array. Sure in_array not worked.

Just replace $map_set[] = $row; by $map_set[] = $row["MapId"]; and then try again.

Upvotes: 3

Related Questions