<?php
    header("Content-Type: application/vnc.ms-excel");
    header("Content-Disposition: attachment; Filename = ".ucwords(strtolower($inputtype->InputType))."-".date('dmYHis').".xls");

    $rows = count($demands);
    if($rows > 0 ){
         $cols = count($demands[key($demands)]);
    };
?>

<table>
<?php 
    $recipe = key($demands);
    $coffees  = $demands[$recipe];
?>
    <tr>
    <td></td>

<?php  for ($i=0; $i < $cols; $i++): ?> 
<?php $coffee = key($coffees); ?>
<td><?=$coffee ?></td>
<?php next($coffees); ?>
<?php endfor; ?>
</tr>

<?php foreach ($demands as $demand): ?>
    <?php $recipe = key($demands) ?>
    <tr>
    <td><?=$recipe?></td>
    <?php $coffees  = $demands[$recipe]; ?>
    <?php for ($i=0; $i < $cols; $i++) { 
        $demand = number_format($coffees[key($coffees)],2);
        echo '<td>'.$demand.'</td>';
        next($coffees);
    }; ?>
    </tr>
    <?php next($demands); ?>
<?php endforeach; ?>

</table>


