if(!is_array($agents) || empty($agents)) { // dizi değil veya içi boş } else { foreach($agents as $agent_id => $agent).... }