Please I tried comparing values in two arrays using in_array(). $friends[] contains all ids of my friends in the friends table while $membersarray[] contains ids of people in a team.

What i'am trying to do here using in_array() is that I want only friends ids who are not part of the team to show up and it worked. But now, I want the result ($t) to display the surnames stored in the friends table instead of just the ids.

For example, it shows: 3, 5 and 7. I want it to show, 'john', 'doe' and 'rick'. How do I do this?

$friends[] = $record5['id']; //contains of ids of all friends

$membersarray[] = $output['id']; //contains ids of all team members

foreach ($friends AS $t) {

   if (in_array($t, $membersarray)) {
   //donothing                         
   } else {
     echo $t;

     }
  }
php 

Create $friends like this:

foreach($results as $row)
{
    $friends[$row['id']]['first_name'] = $row['first_name'];
    $friends[$row['id']]['last_name']  = $row['last_name'];
}

So you save the user ID as index key of the array, when you want to compare use array_keys() to generate an array of IDs:

$friend_keys = array_keys($friends);

foreach($friend_keys as $key)
{
    if( ! in_array($key, $membersarray))
    {
        echo $friends[$key]['first_name'] . ' ' . $friends[$key]['last_name'];
    }
}

Or simply:

foreach($friends as $key => $value)
{
    if( ! in_array($key, $membersarray))
    {
        echo $value['first_name'] . ' ' . $value['last_name'];
    }
}

Thank you. It worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.