PHPda ikkita ichki tsikllar yordamida HTML jadval yaratish

Keling, jadvalimizni ikkita ichki tsikllar yordamida shakllantiramiz:

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

Bunday usul qulay, chunki jadvalning har bir katagini alohida belgilash shart emas. Biroq, bunday yondashuvning kamchiligi nazoratni yo'qotishdir.

Shunday bo'lsa-da, nazoratni shartlar yordamida qaytarish mumkin, mana bunday:

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>';
?>

Kodimizni quyidagicha soddalashtirish mumkin:

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

Quyidagi massiv berilgan:

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

U yordamida HTML jadval yarating.