If the group is empty, the getAudience doesn't seem to return an array. I'm getting "Warning: array_keys() expects parameter 1 to be array, null given in ...Warning: in_array() expects parameter 2 to be array, null given in..."

Maybe wrap this section in something like this -  if (is_array($a->getAudienceMembers())) { .... }