0

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 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.