Fetch membership type along with the contact details with civicrm_api
You need to do it the other way around. Membership is the main entity and then you join on Contact. The api explorer can help put it together but you're looking for something like:

$result = civicrm_api3('Membership', 'get', [
  'sequential' =&gt; 1,
  'return' =&gt; ["contact_id.id", "contact_id.display_name", "membership_type_id"],
]);