<?php
function array_sort($array, $key, $sort = 'desc'){
$tmpArray = $resultArray = array();
foreach ($array as $k => $v){
$tmpArray[$k] = $v["$key"];
}
strtolower($sort) == 'asc' ? asort($tmpArray) : arsort($tmpArray);
foreach ($tmpArray as $k => $v){
$resultArray[$k] = $array[$k];
}
return $resultArray;
}
$array = array(
array('time' => '2010-12/2012-10','desc' => 'a'),
array('time' => '2013-12/2015-10','desc' => 'b'),
);
print_r(array_sort($array, 'time', 'desc'));
输出:Array ( [1] => Array ( [time] => 2013-12/2015-10 [desc] => b ) [0] => Array ( [time] => 2010-12/2012-10 [desc] => a ) )
function array_sort($array, $key, $sort = 'desc'){
$tmpArray = $resultArray = array();
foreach ($array as $k => $v){
$tmpArray[$k] = $v["$key"];
}
strtolower($sort) == 'asc' ? asort($tmpArray) : arsort($tmpArray);
foreach ($tmpArray as $k => $v){
$resultArray[$k] = $array[$k];
}
return $resultArray;
}
$array = array(
array('time' => '2010-12/2012-10','desc' => 'a'),
array('time' => '2013-12/2015-10','desc' => 'b'),
);
print_r(array_sort($array, 'time', 'desc'));
输出:Array ( [1] => Array ( [time] => 2013-12/2015-10 [desc] => b ) [0] => Array ( [time] => 2010-12/2012-10 [desc] => a ) )