Need to retrieve IDs from $wpdb but have multiple parameters


What i am trying to achieve is to hyperlink or profile link the alternative contacts that people have written on their profiles.

I have written this code so far that successfully retrieves the alternative contact of the user. The result of $ names is “Joe Bloggs, John Smith” for example as it is stored as a metakey value in one cell on the database.

add_shortcode( 'Alternative_Contact_Links', function () { global $  current_user; global $  current_user_manager; global $  wpdb; global $  details;  $  contacts = ''; $  contactsnames = ''; $  dummyurl = "https://altranet-test/user/"; $  displaycontacts = '';  $  urldata = parse_url($  _SERVER['REQUEST_URI'], PHP_URL_PATH ); $  urldata = preg_replace('/\D/', '', $  urldata);  $  current_user = wp_get_current_user(); $  current_user_manager =get_metadata( 'user', $  urldata, 'user_id', true );  $  sqlquery = "SELECT DISTINCT wp_usermeta.meta_value, wp_usermeta.user_id FROM wp_usermeta WHERE  wp_usermeta.meta_key = 'Alternative_Contact' AND wp_usermeta.user_id = '$  urldata'";  $  usernames = array(); $  nameArray = array(); $  usernames = $  wpdb->get_results($  sqlquery);  foreach ($  usernames as $  details) { $  names = $  details->meta_value; echo $  names; }  $  sqlquery2 = "SELECT wp_users.ID FROM wp_users WHERE wp_users.display_name IN ('$  usernames')"; echo "<br>". $  sqlquery2; }); 

The problem I am having is that I now need to retrieve the user IDs of Joe and John. However, How do i pass their names into the sqlquery2 each time? Do i need some sort of loop? this is where I am lost.

Any help would be great!

Thanks