<html>
<body bgcolor=lightyellow>

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

<?php

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

//  Print the original table ---------------------------------------------

    print "<b>The Original Students Array: </b><br><pre>";
    print_table( $students );                           //call the print_table() function
    print "</pre><hr/>";                                //see below

//  Sort it --------------------------------------------------------------

    usort($students, 'by_lname_fname');                 //function by_last_name, see below    
    
    print "<b>After Sorting by Lastname then Firstname: </b><br><pre>";
    print_table( $students );
    print "</pre><hr/>";

//  Sort it --------------------------------------------------------------

    usort($students, 'by_lname_fname_desc');    
    
    print "<b>After Sorting by Lastname then Firstname (Descending): </b><br><pre>";
    print_table( $students );
    print "</pre><hr/>";

//  Sort it --------------------------------------------------------------

    usort($students, 'by_lname_ignore_case');    
    
    print "<b>After Sorting by Lastname (Ignoring Case): </b><br><pre>";
    print_table( $students );
    print "</pre><hr/>";

//  Sort it --------------------------------------------------------------

    usort($students, 'by_id_desc');    
    
    print "<b>After Sorting by id (Descending): </b><br><pre>";
    print_table( $students );
    print "</pre><hr/>";
    
//---------------------------------------------------------------------------------------
//Compare function: will be called by the PHP usort function
//   It receives 2 elements.  Since this is a 2 dimensional array, it receives 2 arrays.
//   The job is to tell usort whether to switch the elements by returning 1,
//   or to keep them as is by returning -1 or 0
//---------------------------------------------------------------------------------------
function by_lname_fname($a, $b)         //receive 2 arrays
{
    if ($a['lname'] >  $b['lname']) return  1;  //if $a lname > $b lname -> switch
    if ($a['lname'] <  $b['lname']) return -1;  //if $a lname < $b lname -> no switch
    if ($a['lname'] == $b['lname'])             //if $a lname = $b lname
    {
    if ($a['fname'] >  $b['fname']) return  1;  //if $a fname > $b fname -> switch      
    if ($a['fname'] <  $b['fname']) return -1;  //if $a fname < $b fname -> no switch
    if ($a['fname'] == $b['fname']) return  0;  //no switch
    }
}

function by_lname_fname_desc($a, $b)        //receive 2 arrays
{
    if ($a['lname'] >  $b['lname']) return  1;          //if $a lname > $b lname -> switch
    if ($a['lname'] <  $b['lname']) return -1;          //if $a lname < $b lname -> no switch
    if ($a['lname'] == $b['lname'])                     //if $a lname = $b lname
    {
        if ($a['fname'] >  $b['fname']) return  1;      //if $a fname > $b fname -> switch      
        if ($a['fname'] <  $b['fname']) return -1;      //if $a fname < $b fname -> no switch
        if ($a['fname'] == $b['fname']) return  0;      //no switch
    }
}  

function by_lname_ignore_case($a, $b)      //receive 2 arrays
{
    $lname1 = strtolower($a['lname']);        //convret to lower case
    $lname2 = strtolower($b['lname']);        //convret to lower case

    if ($lname1 >  $lname2) return  1;        //if $a lname > $b lname -> switch
    if ($lname1 <  $lname2) return -1;        //if $a lname < $b lname -> no switch
    if ($lname1 == $lname2) return  0;        //if $a lname = $b lname -> no switch
}

function by_id_desc($a, $b)                     //receive 2 arrays
{
    return ( $b['id'] - $a['id'] );             //if $b > $a return positive -> switch
} 

//---------------------------------------------------------------------------------------
//Print 2 dimensional array as an HTML table
//---------------------------------------------------------------------------------------
function print_table($array_2dim)
{
    print "<table border=2>";
    print "<tr bgcolor=cyan>";

    $keys = array_keys($array_2dim[0]);         //get all the keys of row 0 
    foreach ($keys as $key)                     //keys could be default 0 based
        print "<th>$key</th>";                  //or your own keys
    print "</tr>";

    for ($i = 0; $i < count($array_2dim); $i++)     //loop for number of rows                
    {
    print "<tr>";

    foreach ($array_2dim[$i] as $value)     //loop for number of columns
        print "<td>$value</td>";            //print each value

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

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