将变量传递给array_multisort

时间:2018-06-27 18:10:57

标签: php sorting

我正在尝试创建多重排序方法。以下工作有效,但是我不知道如何将array_multisortSORT_DESC等变量作为变量传递给SORT_ASC,因此我不得不使用下面的if语句。有人知道如何正确执行此操作吗?我正在使用PHP 5.6。

示例:

twoColumnMultiSort($test, 'model', 'year','desc','asc');

功能:

function twoColumnMultiSort(&$arr, $sort1 = '', $sort2 = '', $sort1_type = 'asc', $sort2_type = 'asc')
{
    foreach ($arr as $key => $row) {
        $arr_sort1[$key] = $row[$sort1];
        $arr_sort2[$key] = $row[$sort2];
    }

    $sort1_type = strtolower($sort1_type);
    $sort2_type = strtolower($sort2_type);

    if ($sort1_type == 'asc' && $sort2_type == 'asc') {
        array_multisort($arr_sort1, SORT_ASC, $arr_sort2, SORT_ASC, $arr);
    } else if ($sort1_type == 'asc' && $sort2_type == 'desc') {
        array_multisort($arr_sort1, SORT_ASC, $arr_sort2, SORT_DESC, $arr);
    } else if ($sort1_type == 'desc' && $sort2_type == 'asc') {
        array_multisort($arr_sort1, SORT_DESC, $arr_sort2, SORT_ASC, $arr);
    } else if ($sort1_type == 'desc' && $sort2_type == 'desc') {
        array_multisort($arr_sort1, SORT_DESC, $arr_sort2, SORT_DESC, $arr);
    }

    array_multisort($arr_sort1, SORT_ASC, $arr_sort2, SORT_ASC, $arr);
    return $arr;
}

测试:

$test = array(
    0 => array (
            'id' => 1,
            'model' => 'cayman',
            'year' => '2018',
            'order' => 6,
    ),
    1 =>
        array (
            'id' => 6,
            'model' => '911',
            'year' => '2012',
            'order' => 3,
        ),
    2 =>
        array (
            'id' => 3,
            'model' => 'macan',
            'year' => '2010',
            'order' => 1,
        ),
    3 =>
        array (
            'id' => 5,
            'model' => 'cayman',
            'year' => '1999',
            'order' => 3,
        ),
    4 =>
        array (
            'id' => 4,
            'model' => 'cayman',
            'year' => '2016',
            'order' => 2,
        ),
);

所需的更改直接作为变量进行排序:

$sort1_type = "SORT_DESC";
$sort2_type = "SORT_ASC";
twoColumnMultiSort($test, 'model', 'year',$sort1_type,$sort2_type);

并因此删除方法中的if语句。

2 个答案:

答案 0 :(得分:2)

不要在名称两边加上引号。

$sort1_type = SORT_DESC;
$sort2_type = SORT_ASC;
twoColumnMultiSort($test, 'model', 'year',$sort1_type,$sort2_type);

然后在功能中可以按给定使用它们。

function twoColumnMultiSort(&$arr, $sort1 = '', $sort2 = '', $sort1_type = SORT_ASC, $sort2_type = SORT_ASC)
{
    foreach ($arr as $key => $row) {
        $arr_sort1[$key] = $row[$sort1];
        $arr_sort2[$key] = $row[$sort2];
    }

    array_multisort($arr_sort1, $sort1_type, $arr_sort2, $sort2_type, $arr);

    return $arr;
}

答案 1 :(得分:1)

sort选项不是特殊的关键字,它们只是PHP定义的常量,因此您不必记住实际的值,即实际的数字。

echo SORT_ASC; // 4
echo SORT_DESC; // 3

因此,您可以将它们分配给变量或将它们传递给参数,就像其他值一样:

$sort1_type = SORT_DESC;
$sort2_type = SORT_ASC;
twoColumnMultiSort($test, 'model', 'year',$sort1_type,$sort2_type);

您也可以将它们用作函数定义中的默认值:

function twoColumnMultiSort(&$arr, $sort1 = '', $sort2 = '', $sort1_type = SORT_ASC, $sort2_type = SORT_ASC)