Just a quick code snippet here. I had been looking for a reasonable way to sort an array by the values that are inside it. For example if your array is:
$array = array(
'id' => 1, 'name' => 'Graham',
'id' => 2, 'name' => 'Will',
'id' => 3, 'name' => 'Ryan',
);
But you want your array to be order by name; in this order:
$array = array(
'id' => 1, 'name' => 'Graham',
'id' => 3, 'name' => 'Ryan',
'id' => 2, 'name' => 'Will',
);
Here is a quick function to help you out. It will also sort in reverse order if you like.
function record_sort($records, $field, $reverse=false)
{
$hash = array();
foreach($records as $record)
{
$hash[$record[$field]] = $record;
}
($reverse)? krsort($hash) : ksort($hash);
$records = array();
foreach($hash as $record)
{
$records []= $record;
}
return $records;
}
Connect on LinkedIn. Follow me on Twitter. Grab the RSS Feed