I am looking to go from:
array($ID => array (name => $name, created => $timestamp))
e.g
[5632][name] = martin
[5开发者_开发百科632][created] = 131232342
[6742][name] = paul
[6742][created] = 131232312
[6321][name] = peter
[6321][created] = 131232311
to an array of ids ordered by creation like
[0] = 6321
[1] = 6742
[2] = 5632
What is the fastest way to achieve such in PHP?
function sort_by_date($a, $b)
{
if ($a['created'] == $b['created']) return 0;
return ($a['created'] < $b['created']) ? -1 : 1;
}
$array = array(...);
uasort($array, "sort_by_date");
$ids = array_keys($array);
uasort
lets you sort an array by using a custom function while maintaining keys. array_keys
returns an array containing the keys of another array.
Why go through the effort of sorting the entire array of arrays, when you just want the ids?
$times = array();
foreach ($array as $key => $item) {
$times[$key] = $item['created'];
}
asort($times);
$ids = array_keys($times);
$new_array = krsort(array_keys($your_array));
精彩评论