Below is a variation on Example #4 that implements a multi-key natural sort on an associative array and can be called in such a way as to allow reversing the sort order and/or making the comparison case insensitive on a key by key basis.
Test code is included in the file – run it from the command line with: php sortUtils.php
* sortUtils.php
*/
/**
* mknatsort() - Multi-Key Natural Sort for associative arrays
*
* Uses the usort() function to perform a natural sort on a multi-dimensional
* array on multiple keys. Optionally specifying the sort order for each key
* and/or ignoring the case for each key value.
*
* @param array $data_array The array to be sorted.
* @param mixed $keys The list of keys to be sorted by. This may be a single
* key or an array of keys
* @param boolean $reverse Specify whether or not to reverse the sort order. If
* there are multiple keys then $reverse may be an array of booleans - one
* per key.
* @param boolean $ignorecase Specify whether or not to ignore the case when
* comparing key values.reverse the sort order. If there are multiple keys
* then $ignorecase may be an array of booleans - one per key.
*/functionmknatsort( &$data_array,$keys,$reverse=false,$ignorecase=false) {// make sure $keys is an arrayif (!is_array($keys))$keys= array($keys);usort($data_array,sortcompare($keys,$reverse,$ignorecase) );
}
functionsortcompare($keys,$reverse=false,$ignorecase=false) {
return function ($a,$b) use ($keys,$reverse,$ignorecase) {$cnt=0;// check each key in the order specifiedforeach ($keysas$key) {// check the value for ignorecase and do natural compare accordingly$ignore=is_array($ignorecase) ?$ignorecase[$cnt] :$ignorecase;$result=$ignore?strnatcasecmp($a[$key],$b[$key]) :strnatcmp($a[$key],$b[$key]);// check the value for reverse and reverse the sort order accordingly$revcmp=is_array($reverse) ?$reverse[$cnt] :$reverse;$result=$revcmp? ($result* -1) :$result;// the first key that results in a non-zero comparison determines
// the order of the elementsif ($result!=0) break;$cnt++;
}
return$result;
};
}// end sortcompare()
// test code that will be skipped if this is included in the web environment
// test with: php sortUtils.php from the command line$sapi_type=php_sapi_name();
if (substr($sapi_type,0,3) =='cli') {// the following shows several examples of usage and the resulting output.$test[] = array('Status'=>'In Progress','comment'=>'This looks good.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:20 AM','role'=>'Senior Account Manager');$test[] = array('Status'=>'In Progress','comment'=>'This looks good, please approve.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:19 AM','role'=>'Account Manager');$test[] = array('Status'=>'In Progress','comment'=>'This looks good.','user_name'=>'Jane Smith','date_modified'=>'06/13/2014 11:19 AM','role'=>'Senior Account Manager');$test[] = array('Status'=>'Returned','comment'=>'There is not enough informartion. Please add the following.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:15 AM','role'=>'Account Manager');$test[] = array('Status'=>'In Progress','comment'=>'I am currently reviewing this.','user_name'=>'John Doe','date_modified'=>'06/13/2014 11:14 AM','role'=>'Account Manager');
echo"Original array: ";print_r($test);
echo"Sorted on date_modified: ";mknatsort($test, array('date_modified') );print_r($test);
echo"Sorted on role(reversed) & date_modified: ";mknatsort($test, array('role','date_modified'),array(true,false) );print_r($test);
echo"Sorted on role & date_modified(reversed): ";mknatsort($test, array('role','date_modified'),array(false,true) );print_r($test);
}?>