0

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 
2
Contributors
2
Replies
17
Views
2 Years
Discussion Span
Last Post by uchejava
0

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'];
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.