Get string from array and start loop [closed]

Interesting! Looking at the bp_group_has_members it allows to to pass only single group using the group_id parameter when my first thought was that it would be able to pass an array of groups.
using your method you will need to run the loop multiple times in order to get the results you want.
What i’d suggest is first getting the groups id of the logged in user as you did above:

$user_id = bp_loggedin_user_id();
$groups = BP_Groups_Member::get_group_ids( $user_id );
$groups = $groups['groups'];

or using a direct SQL statement which i believe will be faster:

$groups = $wpdb->get_col($wpdb->prepare("SELECT group_id FROM {$wpdb->prefix}bp_groups_members WHERE user_id = %d",$user_id),0);

Then getting all the uses belong to the each of the groups using a direct SQL statement:

global $wpdb;

$allUsers = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT user_id FROM {$wpdb->prefix}bp_groups_members WHERE group_id IN (".implode(', ', array_fill(0, count($groups), '%s')).")",$groups),0);

and display the results using regular bp_has_members() loop while including the array of the users id that we got from the SQL statement above:

<?php if ( bp_has_members( array( 'include' => $allUsers ) ) ) : ?>

    <?php while ( bp_members() ) : bp_the_member(); ?>

        <li>
            user info....

        </li>

    <?php endwhile; ?>
    </ul>

<?php endif; ?>

You can take a look here to see the bp_has_memebrs loop and to get all the functions you need in order to display the information you want.