原文:http://www.oschina.net/code/snippet_205403_35826
function list_sort_by($list, $field, $sortby = 'asc'){
if (is_array($list)){
$refer = $resultSet = array();
foreach ($list as $i => $data)
{
$refer[$i] = &$data[$field];
}
switch ($sortby){
case 'asc': // 正向排序
asort($refer);
break;
case 'desc': // 逆向排序
arsort($refer);
break;
case 'nat': // 自然排序
natcasesort($refer);
break;
}
foreach ($refer as $key => $val){
$resultSet[] = &$list[$key];
}
return $resultSet;
}
return false;
}
测试:
$list = array(
0 => array(
'id' => 1,
'name' => '第一'
),
1 => array(
'id' => 3,
'name' => '第三'
),
2 => array(
'id' => 2,
'name' => '第二'
),
3 => array(
'id' => 4,
'name' => '第四'
),
);
//解答
$new_list = list_sort_by($list, 'id', 'desc');
echo "<pre>";
var_dump($new_list);