Last active
March 4, 2016 17:41
-
-
Save vinyvicente/410669c799281ec466b2 to your computer and use it in GitHub Desktop.
Array Sort Column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $column | |
* @param array $pieces | |
* @param int $direction (SORT_ASC | SORT_DESC) | |
* @return mixed | |
* @throws Exception | |
*/ | |
function array_sort_column($column, $pieces, $direction = SORT_ASC) | |
{ | |
$filter = []; | |
foreach ($pieces as $key => $value) { | |
if (!isset($value[$column])) throw new Exception('Missing column name.'); | |
$filter[$key] = $value[$column]; // which column | |
} | |
array_multisort($filter, $direction, $pieces); | |
return $pieces; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment