<html>
<body bgcolor=lightyellow>

<H1 Align=center>2 Dimenstional Array</h1>

<?php
    $students = [
        ['id'=>1, 'lname'=>'Burns',    'fname'=>'Barbara', 'ssn'=>'000-01-0001', 'sex'=>'F'],
        ['id'=>2, 'lname'=>'Cambria',  'fname'=>'Vincent', 'ssn'=>'000-01-0002', 'sex'=>'M'],
        ['id'=>4, 'lname'=>'Davidson', 'fname'=>'Duncan',  'ssn'=>'000-01-0004', 'sex'=>'M'],
        ['id'=>4, 'lname'=>'Runyan',   'fname'=>'David',   'ssn'=>'000-01-0004', 'sex'=>'M'],
        ['id'=>5, 'lname'=>'Thomas',   'fname'=>'Eugene',  'ssn'=>'000-01-0005', 'sex'=>'M'],
        ['id'=>6, 'lname'=>'Owens',    'fname'=>'Cynthia', 'ssn'=>'000-01-0006', 'sex'=>'F']
    ];  

//  Another way to define a 2-dimensional array
//  $student1 = ['id'=>1, 'lname'=>'Burns',    'fname'=>'Barbara', 'ssn'=>'000-01-0001', 'sex'=>'F'];
//  $student2 = ['id'=>2, 'lname'=>'Cambria',  'fname'=>'Vincent', 'ssn'=>'000-01-0002', 'sex'=>'M'];
//  $student3 = ['id'=>4, 'lname'=>'Davidson', 'fname'=>'Duncan',  'ssn'=>'000-01-0004', 'sex'=>'M'];
//  $student4 = ['id'=>4, 'lname'=>'Runyan',   'fname'=>'David',   'ssn'=>'000-01-0004', 'sex'=>'M'];
//  $student5 = ['id'=>5, 'lname'=>'Thomas',   'fname'=>'Eugene',  'ssn'=>'000-01-0005', 'sex'=>'M'];
//  $student6 = ['id'=>6, 'lname'=>'Owens',    'fname'=>'Cynthia', 'ssn'=>'000-01-0006', 'sex'=>'F'];
//  $students = [$student1, $student2, $student3, $student4, $student5, $student6];

    print "<b>The original 2 dim students array: </b><br><pre>";
    print_r( $students );
    print "</pre><hr/>";

    $howMany = count($students);                     //result 6
    print "<b>Number of students (rows):</b> $howMany <br/>";
    print "<hr/>";

    $howMany = count($students[0]);                  //result 5
    print "<b>Number of elements for the first student (columns):</b> $howMany <br/>";
    print "<hr/>";
 
//  Print entire first row -------------------------------------------------------

    print "<b>Print entire 1<sup>st</sup> row</b><br><br>";    
    print "<table border=1>";

    $row1   = $students[0];                             //first row
    $keys   = array_keys($row1);                        //get the keys                          
    $values = array_values($row1);                      //get the values                                
   
    print "<tr bgcolor=cyan>";
    foreach ($keys as $k)                       //loop through the keys
        print "<th> $k </th>";                  //print the keys

    print "<tr>";
    foreach ($values as $v)                     //loop through the values
        print "<td> $v </td>";                  //print the keys

    print "</table>";
    print "<hr/>";

//  Print the second column of every row ---------------------------------------------

    print "<b>Print 2<sup>nd</sup> column for all rows</b><br><br>";    

    print "<table border=1>";

    for ($row = 0; $row < count($students); $row++)             //loop for all rows              
        print "<tr><th bgcolor=tan>row $row"  
            . "<td>" . $students[$row]['lname'];                //print lname

    print "</table>";
    print "<hr/>";

//  Print entire 2 dimensional array in an HTML table ----------------------------------

    print "<b>Print 2 dimensional array using HTML table </b><br><br>";    
    print "<table border=2 bgcolor=dddddd>";
    print "<tr bgcolor=cyan>";

    $keys = array_keys($students[0]);               //get all the keys of row 0 
    foreach ($keys as $key)
        print "<th>$key</th>";                      //print table header line
    print "</tr>";

    for ($row = 0; $row < count($students); $row++)       //loop for number of students                
    {
        print "<tr>";
        foreach ($students[$row] as $value)         //loop for each element
            print "<td>$value</td>";                //print the value

//(or)  $values = array_values($students[$row]);    //get all values for each row 
//      foreach ($values as $value)                 //loop for each element
//          print "<td>$value</td>";                //print the value

        print "</tr>";
    }
    print "</table>";
    print "<hr/>";

?>

<?php include "../include.php"; ?>      <!-- hyperlink to see the code -->
</body>
</html>