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:
<?php
function do_result_sort($arr_data, $str_column, $bln_desc = false)
{
$arr_data = (array) $arr_data;
$str_column = (string) trim($str_column);
$bln_desc = (bool) $bln_desc;
$str_sort_type = ($bln_desc) ? SORT_DESC : SORT_ASC;
foreach ($arr_data as $key => $row)
{
${$str_column}[$key] = $row[$str_column];
}
array_multisort($$str_column, $str_sort_type, $arr_data);
return $arr_data;
}
?>
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:
< ?php
// Here's an array...
$arr_test = array(array('name' => 'tim', 'veg' => 'carrots'),
array('name' => 'rob', 'veg' => 'swede'),
array('name' => 'mark', 'veg' => 'brocolli'),
array('name' => 'apaul', 'veg' => 'sprouts'),
array('name' => 'russell', 'veg' => 'mushrooms'),
array('name' => 'stupot', 'veg' => 'leeks'));
// Stick it through our function to sort it...
$arr_test = do_result_sort($arr_test, 'name');
?>
So Microsoft have finally released a developer toolbar for IE and I’ve just installed it (after a c**king reboot! WTF?).
Unfortunately, although it has some nice features (like the Ruler - love it), it still isn’t as comprehensive as Web Developer for Firefox.
Ah well.
Nice start though Microsoft - I’m afraid there’s quite a lot more for you to do to get us developers back into using your browsers though.
The web browser Opera has always been a bit of a niggle with me. It supplies superior options for it’s users as well as some nice advanced accessibility features. Unfortunately, it also featured a nasty banner advert in the top right hand corner of your screen unless you decided to pay a one-off download fee.
However, as of today, that has changed. In a move that will instigate a little competition for the many other free browsers out there (such as Firefox, Safari, Internet Explorer, Netscape etc) Opera has dropped the advert and the download fee.
Avast, ye scurvy dogs!
Today be International Talk Like a Pirate day! So get ye swashbuckling acts together ye band of filthy pigs, afore I cut ye gizzards out!
YAAAAAAAAAARRRRRRRRRRRRR!!!
The new Rentokil Initial website has just gone live. It features some nice text-sizing effects using percentages and ems so that the whole site resizes images and all. We’ve also adopted some degradable javascript “print this page” links and some other nice css effects.
Unfortunately, getting content for the site was something of an up-hill struggle but, thankfully, “those-in-power” have finally cottoned on and are working on it as I type.
It’s also worth noting that the content of the site was an ongoing discussion and, therefore, Paul (our main designer guy) had one hell of a task coming up with the design. How do you design without content?
Stylegala have already featured the site (which was coded as part of my day job in the in-house web team at Rentokil, along with Stuart at Muffin Research Labs) and it’s nice to see differing opinions already being published.
Categories: