<!doctype html>
<html lang="fa">
<head>
	<meta charset="UTF-8">
	
	<style>
		body {
			font: 11px tahoma;
			direction: rtl;
		}
	</style>
</head>
<body>
	<table>
		<?php
			foreach( $users as $index => $user ) {
				
				if( $index == 0 ) {
					echo '<thead><tr>';
					
					foreach( $fields as $key => $field ) {
						
						// key maybe "name of field" and value is a function in this situation
						if( is_numeric( $key ) ) {
							echo "<th>{$field}</th>";
						} else {
							echo "<th>{$key}</th>";
						}

					}
					
					echo '</tr></thead>';
					
					echo '<tbody>';
				}
				
				echo '<tr>';
					$user_array = $user->toArray();
					
					foreach( $fields as $key => $value ) {
						if( is_numeric( $key ) ) {
							echo "<td>" . array_get( $user_array, $value ) . "</td>";
						} else {
							echo "<td>" . $value( array_get( $user_array, $key ) ) . "</td>";
						}
					}
				echo '</tr>';
				
			}
			
			if( count( $users ) ) {
				echo '</tbody>';
			}
		?>
	</table>
</body>
</html>