<html>
<! Array6.php                                                 >
<! Chapter 6, Oracle Database XE PHP Web Programming          >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This declares a nested associative array and assigns all   >
<! elements, then it uses a foreach-loop to mimic the         >
<! print_r() function.                                        >
<head>
<title>
  Chapter 6 : Array6.php
</title>
</head>
<body>
<?php
  // Declare variable.
  $salutation = array
                ("Bilbo"=>array
                          ("Hobbit"=>"Who slew a dragon for dwarves."
                          ,"Fellowship"=>"Left the ring for Frodo."
                          ,"Return of the King"=>"Left middle earth.")
                ,"Frodo"=>array
                          ("Fellowship"=>"Who inherited the ring."
                          ,"Return of the King"=>"Who destroyed the ring.")
                );

  // Declare variable externally to the loop.
  $output = "<table border=0 cellspacing=0>";

  // Read and add array keys and values to a string.
  foreach ($salutation as $hashName => $hashValue)
  {
    // Append the element name.
    $output .= "<tr>";
    $output .= "<td valign=top>[".$hashName."]</td>";

    // Check if element value is a nested array.
    if (is_array($hashValue))
    {
      // Open an HTML table cell and nested table.
      $output .= "<td valign=top><table border=0 cellspacing=0>";

      // Read and add array keys and values to a string.
      foreach ($hashValue as $nestedName => $nestedValue)
      {
        // Append the nested element name and value.
        $output .= "<tr>";
        $output .= "<td valign=top>[".$nestedName."]</td>";
        $output .= "<td valign=top>[".$nestedValue."]</td>";
        $output .= "</tr>";
      }

      // Close nested HTML table and cell.
      $output .= "</table></td>";
    }
    else
    {
      // Append the non-array value element.
      $output .= "<td>[".$hashValue."]</td>";
      $output .= "</tr>";
    }
  }

  // Close HTML table.
  $output .= "</table>";

  // Print the formatted contents.
  print $output;
?>
</body>
</html>
