gopibabus
7/8/2018 - 12:16 AM

Print values from multi-dimentional array

This program will show how to print values from multi-dimentional array to table format

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Print values from multi-dimentional array</title>
</head>
<body>
<?php
$products = array
(
  0 => array
  (
            "id" => 13,
            "category_id" => 7,
            "name" => 'Leaving Of Liverpool',
            "description" => 'Leaving Of Liverpool',
            "price" => 1.00,
            "virtual" => 1,
            "active" => 1,
            "sort_order" => 13,
            "created" => '2007-06-24 14:08:03',
            "modified" => '2007-06-24 14:08:03',
            "image" => "NONE"
  ),
  1 => array 
  (
            "id" => 16,
            "category_id" => 7,
            "name" => 'Yellow Submarine',
            "description" => 'Yellow Submarine',
            "price" => 1.00,
            "virtual" => 1,
            "active" => 1,
            "sort_order" => 16,
            "created" => '2007-06-24 14:10:02',
            "modified" => '2007-06-24 14:10:02',
            "image" => "NONE"
  ) 
  );
  ?>
  <table>
  <?php
  foreach ($products as $key => $value) {
      if($key == 0){
        echo "<th>";
        foreach ($value as $k => $v) {
          echo "<td>$k</td>";
    } 
    echo "</th>";
      }
}
  ?>

  <?php
  foreach ($products as $key => $value) {
        echo "<tr>";
        foreach ($value as $k => $v) {
          echo "<td>$v</td>";
    } 
    echo "</tr>";
}
  ?>
  </table>  
</body>
</html>