정렬 방향에 문제가 있습니다. 방향을 가지고 다차원 배열을 정렬하려고합니다. 나는 얼마나 많은 매개 변수가 될지 모르기 때문에 array_multisort()
을 직접 사용할 수 없습니다. 내가 사용 call_user_func_array('array_multisort', $params);
그리고 그것은 작동하지만, 정렬 방향 (SORT_ASC,SORT_DESC
)를 설정할 수 없습니다. call_user_func_array('array_multisort', $params);
의 정렬 방향을 어떻게 설정할 수 있습니까? 여기 내 코드는, 당신이call_user_func_array with array_multisort
function get_fields($data, $order_by) {
$order_row = preg_split("/[\s,]+/", $order_by);
for ($i=0;$i<count($order_row);$i++) {
foreach ($data as $key => $row) {
$tmp[$i][$key] = $row[$order_row[$i]];
}
}
return $tmp;
}
function ordering($data, $order_by) {
$tmp = get_fields($data, $order_by);
$params = array();
foreach($tmp as &$t){
$params[] = &$t;
}
$params[1] = array("SORT_DESC","SORT_DESC","SORT_DESC","SORT_DESC"); // like that no warning but no sorting
$params[] = &$data;
call_user_func_array('array_multisort', $params);
return array_pop($params);
}
$data = array (
array('id' => 1,'name' => 'Barack','city' => 9),
array('id' => 7,'name' => 'boris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2)
);
$order_by = "city desc, name";
echo "<br>ORDER BY $order_by<br>";
$ordered = ordering($data, $order_by);
echo "<pre>";
var_dump($ordered);
echo "</pre>";
내가 MySQL을 ORDER BY city DESC, name
같은 종류의 작업을 수행하려는 시도 할 수 있습니다. 내 목표 야.