Generarea unui tabel HTML folosind două bucle imbricate în PHP

Să generăm tabelul nostru folosind două bucle imbricate:

php<?php
  echo '<table>';
  foreach ($arr as $row) {
    echo '<tr>';
    foreach ($row as $cell) {
      echo "<td>$cell</td>";
    }
    echo '</tr>';
  }
  echo '</table>';
?>

Acest mod este convenabil deoarece nu este nevoie să scriem fiecare celulă a tabelului separat. Totuși, dezavantajul acestei abordări este pierderea controlului.

Cu toate acestea, controlul poate fi recuperat cu ajutorul condițiilor, astfel:

php<?php
  echo '<table>';
  foreach ($arr as $row) {
    echo '<tr>';
    foreach ($row as $key => $cell) {
      if ($key === 'salary') {
        echo "<td>$cell dollars</td>";
      } else {
        echo "<td>$cell</td>";
      }
    }
    echo '</tr>';
  }
  echo '</table>';
?>

Putem simplifica codul nostru în felul următor:

php<?php
  echo '<table>';
  foreach ($arr as $row) {
    echo '<tr>';
    foreach ($row as $key => $cell) {
      if ($key === 'salary') {
        $cell .= ' dollars';
      }
      
      echo "<td>$cell</td>";
    }
    echo '</tr>';
  }
  echo '</table>';
?>
Задание 1

Se dă următorul array:

php<?php
  $products = [
    [
      'name'   => 'product1',
      'price'  => 100,
      'amount' => 5,
    ],
    [
      'name'   => 'product2',
      'price'  => 200,
      'amount' => 6,
    ],
    [
      'name'   => 'product3',
      'price'  => 300,
      'amount' => 7,
    ],
  ];
?>

Generați un tabel HTML folosindu-l.