OK...
I have a multidimensional array...
Like this...


$output[0][0] = 111;
$output[0][1] = 'Joe';
$output[0][2] = 'Blow';
$output[1][0] = 112;
$output[1][1] = 'Jane';
$output[1][2] = 'Doe'; 

And so on...

I can get the array to display in a table with no problems...
Now what I want it to do is order the table by Last Name which is in the following location...


// If $i is the current 'row' being displayed...

$output[$i][2] = "Last Name";

I found the following function in Php.net but cannot get it to work...


function multisort(&$array, $sortby, $order='ASC') {
   $order_s = SORT_DESC;
   if ($order == 'ASC') {
       $order_s = SORT_ASC;
   }
   if (!isset($array -> $sortby))
       return;
   asort($array->$sortby, $order_s);
   $sorting = array();
   foreach ($array -> $sortby as $key => $val) {
       $sorting[] = $key;
   }
   foreach ($array as $key => $val) {
       //sort key
       $valaux = array();
       for($i=0; $i<count($sorting); $i++) {
           $valaux[] = $val[$sorting[$i]];
       }
       $array->$key = $valaux;
   }
} 

I copied and pasted this into my code and used the following to call the function...


// Lets see if this function works

   multisort($output, 3, 'DESC');  

When I run the page, nothing changes...
I still get the same order...
At least it doesn't just give me a blank page...!

Can anyone see what I'm doing wrong here...?

Any help would be greatly appreciated...!!!!!!

    Here is an update...
    I've been searching around and found some code snippits that work like I want...
    Here's what I found...

    
    function cmp ($a,$b)  
    {  
      $cmp = strcmp($a[4],$b[4]); 
      return $cmp ? $cmp : -1; 
    }
    
    // Now let's see if this function works...
    
    usort($output,"cmp");      
    
    
    

    I'm not sure how all this works but it did...
    I can tell you this...

    In the line...

    $cmp = strcmp($a[4],$b[4]);
    

    [4] is the column I need to sort by...

    Also, in the line...

    usort($output,"cmp");
    

    $output is the name of the multi-dimensional array that I am sorting...


    Now the problem I am running into is that the values in column [4] are numbers. When the array is sorted, I get the following...

    1
    10
    11
    12
    ...
    2
    20
    21
    ... etc

    How can I get this to sort the values as numbers...?

    Also, I need it to be sorted DESC...
    How do I add this...?

      I was bored so I hacked this together. It's probably not the most efficient code in the world, but it works:

      <?php
      function multi_sort(&$array,$column=0,$order="ASC") {
          //make sure an array is passed as the first parameter
          if(!is_array($array)) {return FALSE;}
      
      $keys  = array_keys($array);
      $order = strtoupper($order);
      $temp  = array();
      $column--;
      
      //make sure array is multidimensional and each entry has at
      //least $column number of entries
      foreach($keys as $key) {
          if(!is_array($array[$key]) || count($array[$key]) < $column || $column < 0) {
              return FALSE;
          } else {
              $temp[$key] = $array[$key][$column];
          } //end if
      } //end foreach
      
      
      if($order != 'ASC' && $order != 'DESC') {return FALSE;}
      
      //sort the 1d array
      if($order == 'ASC') {asort($temp);}
      else {arsort($temp);}
      
      $temp2 = array();
      $temp3 = array_keys($temp);
      
      foreach($temp3 as $key) {
          $temp2[] = $array[$key];
      } //end foreach
      
      $array = $temp2;
      
      return TRUE;
      } //end multi_sort
      ?>

      A word of warning it takes array as a reference so it will destroy the array passed to it and it does not return an array but simply modifies the one passed to it, just as asort and arsort do.

      Also column is 1 indexed not 0 indexed, just made more sense to me that way.

      Finally if you pass it an associative array it will sort it but your keys will be lost.

        I got this to work...!
        I'm putting the code for the page below in case it will help anyone...
        What I did was move the column that I wanted to order by to the $output[$i][0] position instead of the $output[$i][4] spot. This made it sort my the column I wanted automatically...

        Then I used the rsort function to sort it desc...

        
        <?
        
        // Hit up DB...
        
        include("dbConnect.php");
        
        // Set up query...
        
        $results = mysql_query("SELECT * FROM apartments");
        
        // Establish how many rows were returned...
        	$totalrows = mysql_num_rows($results);
        
        
        // Start moving rows through loop...
        
        if($myrow = mysql_fetch_array($results))
        {
        
        	$i = 0;
        
        
        	do
        	{
        
        		// Set outside variable
        		$var = "work";
        
        		// Load information from query and new variable into array
        		$output[$i][0] = $i;
        		$output[$i][1] = $myrow['AptID'];
        		$output[$i][2] = $myrow['AptAddress'];
        		$output[$i][3] = $myrow['AptApt'];
        		$output[$i][4] = $myrow['AptCity'];
        
        
        		if($i<$totalrows)
        		{
        			$i = ($i+1);
        		}
        
        	}
        
        	while ($myrow = mysql_fetch_array($results));	
        
        }
        
        else
        
        {
        
        	echo "Sorry Charlie";
        
        }
        
        
        // Let's put this in a visible table...
        
        $i = 0;
        
        echo "Results";
        
        echo "<table>";
        echo "<tr>
        		<td>
        			Match
        		</td>
        		<td>
        			ID
        		</td>
        		<td>
        			Address
        		</td>
        		<td>
        			Apt
        		</td>
        		<td>
        			City
        		</td>
        	</tr>";
        
        // Sort array desc
        
         rsort($output);	
        
        
        // Send array through the output loop thing
        
        if (count($output) > 0)
        {
        	for ( $i = 0; $i < count($output); $i++ )
        	{
        		echo "<tr>";
        		echo "<td>";
        		echo $output[$i][0];
        		echo "</td><td>";
        		echo $output[$i][1];
        		echo "</td><td>";
        		echo $output[$i][2];
        		echo "</td><td>";
        		echo $output[$i][3];
        		echo "</td><td>";
        		echo $output[$i][4];
        		echo "</td></tr>";
        	}
        
        }
        
        else
        
        {
        
        	echo "Move along, nothing to see here";
        
        }
        
        
        
        ?>				
        
        
          Write a Reply...