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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了