Skip to content

Instantly share code, notes, and snippets.

@tomschlick
Created January 23, 2011 11:26
Show Gist options
  • Select an option

  • Save tomschlick/792004 to your computer and use it in GitHub Desktop.

Select an option

Save tomschlick/792004 to your computer and use it in GitHub Desktop.
This function allows you to sort a php array by subelement values, this should be used in conjunction with the array_element function.
<?php
function array_sort($array, $key, $order = 'asc', $sort_flags = SORT_REGULAR)
{
if( ! is_array($array))
{
return FALSE;
}
foreach($array as $k=>$v)
{
$b[$k] = array_element($v, $key);
}
switch($order)
{
case 'asc':
asort($b, $sort_flags);
break;
case 'desc':
arsort($b, $sort_flags);
break;
}
foreach($b as $key=>$val)
{
$c[$key] = $array[$key];
}
return $c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment