Return member level id from Membership

I have a page that creates a table with user information. Part of the information that should be displayed is the membership level. I am using the following code to return the level for a user given the user ID. There are 4 levels set up and these levels get put into an array and then I use a foreach loop to determine the level. This works fine but for each user, this code loops through several functions multiple times. I am trying to find a more elegant way to do this so that we enumerate each user only once, and then either return the level_id or loop through the four levels to identify which one the user is assigned to.

$levels = array (“3″,”4″,”7″,”8”:wink:;

foreach ($levels as $level) {

$is_on_level = membership_user_on_level($level,$member_id);

If ($is_on_level){

echo “User is on level “.$level;



These functions were adapted from the functions.php file in the includes directory for the membership plugin.

function get_membership_member($member_id){

return Membership_Plugin::factory()->get_member($member_id);


function membership_user_on_level($level_id, $member_id ) {

$member = get_membership_member($member_id);

if ( !empty( $member ) ) {

return $member->on_level( $level_id, true );