PHP 5.4 introduces the useful SORT_FLAG_CASE for making any other search case insensitive. Unfortunately this isn’t available in PHP 5.3 or less and so I was wondering how the following array:
array('a'=>2,'b'=>4,'A'=>1,'B'=>3);
Could be sorted into:
array('A'=>1,'a'=>2,'B'=>3,'b'=>4);
As the usual ksort() function sorts it as:
array('A'=>1,'B'=>3,'a'=>2,'b'=>4);
A comment on one of the PHP function reference pages pointed me to the
uksort()function; this (and theuasort()function for sorting by value instead of key) allow the comparison algorithm for shifting in the quick sort to be written by the user.Combine this with the very simple
strcasecmp()function (which compares two strings and returns <0 for a>b and >0 for a>b) gives you:To easily achieve the effect of:
In PHP 5.3 or less.