skip to content

 Web design 

Database-driven systems

Easy-to-maintain web sites


 



Array Sort

 This is a module to simplify the sorting of arrays. Sorting a multi-dimensional array by multiple key fields isn't something that you need to write that frequently. As a result, you can waste quite a bit of time figuring out how to make it work (I know I did!). Having spent the better part of a day working through this (experimenting with different options), I didn't want to ever have to do that again. I wrote this module to make it easy to do it the next time. I share it here so you can avoid wasting this time as well.

Using it looks like this:

 array_rows_to_columns ($test,"last_name","first_name","address","phone"); 
 $result = array_multisort($sort_field1,$sort_field2,$sort_field3,$sort_field4,$test);  

The array_multisort is standard PHP so the key is the setup to make it work. The array_rows_to_columns uses the approach suggested in the PHP help file to turn the individual fields into separate arrays so they all become sortable. Through the magic of PHP's associative arrays, the sort then works and all the associations remain intact.

The downloadable file includes a demo module.

Download it here

 
   

Powered by Website Baker,
Design by Innovations Design based on a template by gavjof