[FX.php List] Sorting multidimensional array

Chris Hansen chris at iViking.org
Thu Feb 28 09:01:00 MST 2008


Tim,

What you want is PHP's uasort() function.  So, you could sort  
something like this:

function CompareDistances ($a, $b)
{
     if ($a['Distance'] == $b['Distance']) {
         return 0;
     }
     return ($a['Distance'] < $b['Distance']) ? -1 : 1;
}

uasort($theDistances, 'CompareDistances');

The function above was modified from one in the PHP documentation (see  
the usort() entry).  In short, $a and $b will be array elements, and  
we just need to set up a function which specifies what to sort on.  I  
so love PHP =)

--Chris Hansen
   FileMaker 8 Certified Developer
   FileMaker 7 Certified Developer
   Creator of FX.php
   "The best way from FileMaker to the Web."
   www.iViking.org

On Feb 27, 2008, at 5:44 PM, Tim 'Webko' Booth wrote:

> Dear All,
>
> For some reason my brain is not working so well - this is trivial  
> but... I have a multidim array as follows, and I want to sort it on  
> Distance ascending so each sub-record is kept together but they are  
> in order.
>
> Cheers
>
> Webko
>
>
> theDistances
> Array
> (
>     [0] => Array
>         (
>             [recid] => 74
>             [modid] => 11
>             [__kp_ID] => DEA00074
>             [Latitude] => +40.365020
>             [Longitude] => -079.584060
>             [Distance] => 82.148028545362
>         )
>
>     [1] => Array
>         (
>             [recid] => 741
>             [modid] => 11
>             [__kp_ID] => DEA00741
>             [Latitude] => +40.436827
>             [Longitude] => -079.617178
>             [Distance] => 79.391442648614
>         )
>
>     [2] => Array
>         (
>             [recid] => 1145
>             [modid] => 11
>             [__kp_ID] => DEA01145
>             [Latitude] => +40.621272
>             [Longitude] => -079.080422
>             [Distance] => 52.951428710724
>         )
>
> )
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list





More information about the FX.php_List mailing list