Eggzorcist Posted June 26, 2012 Share Posted June 26, 2012 Hi there, I have a query I'm trying to find a userID in. My query is a fetchAll(PDO::FETCH_ASSOC), and want to find $addF = 2. The fetch_all may have 1-10 datasets. Generally lower rather than higher. I'm just unsure why this isn't working. As when I print_r the assoc, I see [id] = 2. a nd my $addF = 2. This is how I'm using the in_array. if(in_array($addF, $penfListData)){...} am I not using it right in the if statement. I want to do if found then follow up on the if logic. Any further advice, guidances would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/264817-finding-a-value-in-an-assoc-using-in_array/ Share on other sites More sharing options...
Eggzorcist Posted June 26, 2012 Author Share Posted June 26, 2012 I have read it may be due since in_array() doesn't deal with associative arrays, and merely array('value'). Since my is an associative array, after a bit of research it was suggested to use array_key_exists(). But this only finds the key, so if I have many array(id => 'value' ) then it would just generelize. That's not what I need. I need to find a specific value within the 'id' key from userprofiles. Quote Link to comment https://forums.phpfreaks.com/topic/264817-finding-a-value-in-an-assoc-using-in_array/#findComment-1357140 Share on other sites More sharing options...
kicken Posted June 26, 2012 Share Posted June 26, 2012 You just have to use a foreach loop to find the value. Such as: $found=null; foreach ($penfListData as $v){ if ($addF == $v['userID']){ $found=$v; break; } } if (!$found){ //id does not exist. } Quote Link to comment https://forums.phpfreaks.com/topic/264817-finding-a-value-in-an-assoc-using-in_array/#findComment-1357143 Share on other sites More sharing options...
jcanker Posted June 26, 2012 Share Posted June 26, 2012 UPDATE: You can probably ignore most of the stuff I posted below. When I finished, I noticed that you're trying to find an ARRAY KEY, not value using in_array(). in_array() only looks at values, not keys. If you want to see if a key exists in an array, use array_key_exists($key,$array). I'm including the rest of what I had answered just in case (and because I spent the time to write it ) This is a function I have in an include file that greatly simplifies the task of parsing out the results of a returned MySQL query. function db_result_to_array($result) { $res_array = array(); for ($count=0; $row = @mysql_fetch_array($result); $count++) $res_array[$count] = $row; return $res_array; } Just run your query: PDO or using mysql_query($query) and whatever the returned results are (here I'll call it $result) $result = db_result_to_array($result); foreach ($result as $row) //this will parse through each row of the returned array, not each column { //this function allows you to access the returned row currently being processed by mysql column name, so you can run your in_array() here: if(in_array($mySearch, $row)){...} } That should work, although I've never used it this way. Because this is coming directly as a query from the DB, you should know which column would contain the value you're looking for. Just run an if statement on that. In the above example, the foreach would contain a line similar to: if($row['nameOfMysqlColumnThatWouldContainTheValueImLookingFor'] == $varNameThatHoldsTheValueImLookingFor){...} Seems to me that's much simpler than searching the array for a value when you know where it will be. NOTE: Also keep in mind that in_array() won't look through lower levels of a multidimentional array. You have to navigate to that level first and then search for it. You can do this in nested while or for loops with a few counters. Quote Link to comment https://forums.phpfreaks.com/topic/264817-finding-a-value-in-an-assoc-using-in_array/#findComment-1357144 Share on other sites More sharing options...
Eggzorcist Posted June 26, 2012 Author Share Posted June 26, 2012 Thanks guys. I wanted to avoid using a loop since I thought it was less efficient. But I guess that's the only way. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/264817-finding-a-value-in-an-assoc-using-in_array/#findComment-1357148 Share on other sites More sharing options...
PFMaBiSmAd Posted June 26, 2012 Share Posted June 26, 2012 But I guess that's the only way. If you are trying to find if a value exists in a database table, you would generally do that in the query itself. Quote Link to comment https://forums.phpfreaks.com/topic/264817-finding-a-value-in-an-assoc-using-in_array/#findComment-1357175 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.