Skip to content

Instantly share code, notes, and snippets.

@vinyvicente
Last active March 4, 2016 17:41
Show Gist options
  • Save vinyvicente/410669c799281ec466b2 to your computer and use it in GitHub Desktop.
Save vinyvicente/410669c799281ec466b2 to your computer and use it in GitHub Desktop.
Array Sort Column
<?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