-- PHP
배열의 데이터를 오름차순, 내림차순으로 정렬
어린왕자악꿍
2013. 4. 11. 15:46
$test = array(
array("name" => "someone1", "reg_date" => "2013-04-01 13:00:00"),
array("name" => "someone2", "reg_date" => "2013-04-01 13:01:00"),
array("name" => "someone3", "reg_date" => "2013-04-01 12:00:00")
);
이와 같은 배열이 있다고 할 때, reg_date별로 정렬하고 싶다면 아래와 같이 한다.
usort($test, function($a1, $a2) {
$v1 = strtotime($a1->reg_date);
$v2 = strtotime($a2->reg_date);
return $v2 - $v1; // $v2 - $v1 : 내림차순, $v1 - $v2 : 오름차순
});
foreach($test as $a) {
echo $a->name . ' ' . $a->reg_date . "<br>";
}
결과)
someone2 2013-04-01 13:01:00
someone1 2013-04-01 13:00:00
someone3 2013-04-01 12:00:00