Sorted
Sooner or later, whilst using arrays in PHP, you’re going to want to re-order the data. On a one-dimensional array this simply requires us to run one of the sort()
, rsort()
, asort()
or ksort()
commands (depending on what we are doing).
However, should we be using a mult-dimensional array, we need to use the array_multisort()
PHP command.
If our array is a database result, it is usually a two-dimensional array arranged in rows but array_multisort()
sorts using columns. For this reason we need to do some hefty work preparing our array for ordering. Because of this, I ended up writing this short function the other day:
This function basically allows me to reorganise a result set array by any column either DESC or ASC (however, only one column at a time - with a little hacking we could change that easily).
Since I’ve found this remarkably useful on occasion, I thought I’d share it with the world…
Update: Just realised that I didn’t give you an example of it in use. Consider that corrected: