[FX.php List] Sorting multidimensional array

Dale Bengston dbengston at preservationstudio.com
Wed Feb 27 19:46:44 MST 2008


OK, here's a better version from something better than my memory. It  
uses the original keys to preserve unique values:

$tempDistances = array();
$sortedDistances = array();

foreach(array_keys($theDistances) as $key)
	{
	$tempDistances[$key] = $theDistances[$key]['Distance'];
	}
			
asort($tempDistances);
			
foreach(array_keys($tempDistances) as $key)
	{
	$sortedDistances[$key] = $theDistances[$key];
	}

Dale

On Feb 27, 2008, at 8:30 PM, Dale Bengston wrote:

> Hi Webko,
>
> I think you have to turn it inside out first, making a new array  
> with the record keys being the field value you want to sort.
>
> $sortedDistances = array();
> foreach ($theDistances as $value)
> 	{
> 	$sortedDistances[$value['Distance']] = $value;
> 	}
>
> asort($sortedDistances);
>
> That's from pseudocode from memory. Also not a real robust solution  
> since it will go wrong if you have two or more identical values for  
> $value['Distance']. Still, maybe it will get your brain back on track.
>
> Hope this helps,
> Dale
>
>
> On Feb 27, 2008, at 6: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
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.iviking.org/pipermail/fx.php_list/attachments/20080227/91a7ded1/attachment.html


More information about the FX.php_List mailing list