Я воспользовался этим, чтобы написать это так, как хотел бы.Надеюсь, это поможет вам.
<?php
$data = [
[
"title" => "The World's End",
"genre" => "Sci-fi",
"year" => 2013,
"gross" => 21,
],
[
"title" => "Scott Pilgrim vs. the World",
"genre" => "Sadness",
"year" => 2010,
"gross" => 21,
],
[
"title" => "Hot Fuzz",
"genre" => "Buddy Cop",
"year" => 2007,
"gross" => 21,
],
[
"title" => "Shaun of the Dead",
"genre" => "Zombie",
"year" => 2007,
"gross" => 21,
],
];
// Let's grab your headers
$headers = array_keys($data[0]);
// Format them by capitalizing the first letter
$headers = array_map('ucfirst', $headers);
/**
* You could also inline it into one assignment
* This decreases readability, but removes the quick reassignment of $header
*
* $headers = array_map('ucfirst', array_keys($data[0]));
*/
/**
* Let's calculate the gross here instead of in the presentation (HTML)
*/
$total_gross = array_reduce($data, function ($gross, $movie) {
$gross += $movie['gross'];
return $gross;
}, 0);
?>
<table>
<tr>
<?php foreach ($headers as $header) :
printf('<th>%s</th>', $header);
endforeach; ?>
</tr>
<?php foreach ($data as $movie) : ?>
<tr>
<?php
printf('<td>%s</td>', $movie['title']);
printf('<td>%s</td>', $movie['genre']);
printf('<td>%s</td>', $movie['year']);
printf('<td>€ %s</td>', $movie['gross']);
?>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="3"></td>
<?php printf('<td>€ %s</td>', $total_gross); ?>
</tr>
</table>