Jump to content

finding a value in an ASSOC using in_array()


Eggzorcist

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
}

Link to comment
Share on other sites

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  :P)

 

 

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.

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.