Skip to content

Instantly share code, notes, and snippets.

@dapepe
Created February 4, 2014 09:13
Show Gist options
  • Save dapepe/8800406 to your computer and use it in GitHub Desktop.
Save dapepe/8800406 to your computer and use it in GitHub Desktop.
Sort an array by key
<?php
/**
* Sorts an array by key
*
* @param array $arrData
* @param string $strKey
* @param bool $bolAsc
*/
function sortArray($arrData, $strKey, $bolAsc=true) {
if(!is_array($strKey))
$strKey = array($strKey);
usort($arrData, function($a, $b) use($strKey) {
$retval = 0;
foreach($strKey as $strKeyname)
if($retval == 0) $retval = strnatcmp($a[$strKeyname],$b[$strKeyname]);
return $retval;
});
return $bolAsc ? $arrData : array_reverse($arrData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment