douqianke7467
douqianke7467
2018-06-27 18:10
浏览 45
已采纳

将变量传递给array_multisort

I'm trying to create a multisort method. The following works but I don't know how to pass array_multisort the variables for SORT_DESC, SORT_ASC etc as variables so I had to use the if statement below. Does anyone know how to do this properly? I am using PHP 5.6.

Example:

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

Function:

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:

$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,
        ),
);

Desired change pass in sorting as variables directly:

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

and hence remove the if statement in the method.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanqianmou4661
    duanqianmou4661 2018-06-27 18:22
    已采纳

    Don't put quotes around the names.

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

    Then in the function you can use them as given.

    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;
    }
    
    点赞 评论
  • ds342222
    ds342222 2018-06-27 18:25

    The sort options aren't special keywords, they are just constants defined by PHP so you don't have to memorise the real values, which are actually numbers.

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

    So you can assign them to a variable or pass them to a parameter like any other value:

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

    You can use them as the default in the function definition as well:

    function twoColumnMultiSort(&$arr, $sort1 = '', $sort2 = '', $sort1_type = SORT_ASC, $sort2_type = SORT_ASC)
    
    点赞 评论

相关推荐