我希望从:
array($ID => array (name => $name, created => $timestamp))
e.g
[5632][name] = martin
[5632][created] = 131232342
[6742][name] = paul
[6742][created] = 131232312
[6321][name] = peter
[6321][created] = 131232311
到按创建排序的id数组,如
[0] = 6321
[1] = 6742
[2] = 5632
在PHP中实现此目的的最快方法是什么?
答案 0 :(得分:3)
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
允许您在维护密钥时使用自定义函数对数组进行排序。 array_keys
返回一个包含另一个数组键的数组。
答案 1 :(得分:1)
当你只想要ids时,为什么还要整理整个数组?
$times = array();
foreach ($array as $key => $item) {
$times[$key] = $item['created'];
}
asort($times);
$ids = array_keys($times);
答案 2 :(得分:0)
$new_array = krsort(array_keys($your_array));