0

我有排序方向的问题。我尝试使用方向对多维数组进行排序。我不能array_multisort()直接使用,因为我不知道会有多少个参数。我使用call_user_func_array('array_multisort', $params);And it works,但我无法设置排序方向 ( SORT_ASC,SORT_DESC)。如何设置排序方向call_user_func_array('array_multisort', $params);?这是我的代码,你可以试试

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。这是我的目标。

4

3 回答 3

1

为了能够对数组进行多次排序并获得类似的结果,ORDER BY city DESC, name ASC您需要一个稳定排序的函数。
据我所知,PHP 没有,所以你必须使用像这样的比较器函数对其进行一次排序

$data = array (
    array('id' => 3,'name' => 'coris','city' => 2),
    array('id' => 1,'name' => 'Barack','city' => 9),
    array('id' => 7,'name' => 'boris','city' => 2),
    array('id' => 3,'name' => 'coris','city' => 2),
);

$order_by = array(
    'city' => array('dir' => SORT_DESC, 'type' => SORT_NUMERIC),
    'name' => array('dir' => SORT_ASC, 'type' => SORT_STRING),
);

function compare($row1,$row2) {
    /* this function should determine which row is greater based on all of the criteria
       and return a negative number when $row1 < $row2
                  a positive number when $row1 > $row2
                  0 when $row1 == $row2
    */

    global $order_by;

    foreach($order_by as $field => $sort) {
        if($sort['type'] != SORT_NUMERIC) {
            // strings are compared case insensitive and assumed to be in the mb_internal_encoding
            $cmp = strcmp(mb_strtolower($row1[$field]), mb_strtolower($row2[$field]));
        } else {
            $cmp = doubleval($row1[$field]) - doubleval($row2[$field]);
        }
        if($sort['dir'] != SORT_ASC) $cmp = -$cmp;
        if($cmp != 0) return $cmp;
    }
    return 0;
}


usort($data,'compare');
于 2012-07-30T13:46:48.543 回答
1

我有同样的问题。似乎 call_user_func_array() 无法处理常量。我通过动态构建一个参数字符串并评估这个字符串解决了这个问题:

$args  = array($arr1, $arr2);
$order = array(SORT_ASC, SORT_DESC);
$evalstring = '';

foreach($args as $i=>$arg){
    if($evalstring == ''){ $evalstring.= ', '; }
    $evalstring.= '$arg';
    $evalstring.= ', '.$order[$i];
}
eval("array_multisort($evalstring);");

我知道 eval() 是邪恶的,这不是一个干净的方法,但它有效;-)

于 2012-12-19T14:28:58.687 回答
0

它对我有用:

$arrayThatNeedToSort = array('data..');
$multiSortprop = array(['data.....']=> SORT_DESC,['data.....'] => SORT_ASC)

$properties = array();
foreach ($multiSortprop as $sortArr => $sortArg) {
         array_push($properties,$sortArr);
         array_push($properties,$sortArg);
 }

array_push($properties,$arrayThatNeedToSort);
array_multisort(...$properties);

var_dump(end($properties));
于 2018-10-03T09:39:14.447 回答