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 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献