Php 将变量传递到数组\ u multisort
我正在尝试创建一个多重排序方法。下面的方法可以工作,但我不知道如何将Php 将变量传递到数组\ u multisort,php,sorting,Php,Sorting,我正在尝试创建一个多重排序方法。下面的方法可以工作,但我不知道如何将array\u multisort的SORT\u DESC、SORT\u ASC等变量作为变量传递,因此我不得不使用下面的if语句。有人知道如何正确地做到这一点吗?我使用的是PHP5.6 例如: twoColumnMultiSort($test, 'model', 'year','desc','asc'); 功能: function twoColumnMultiSort(&$arr, $sort1 = '', $sor
array\u multisort
的SORT\u DESC
、SORT\u ASC
等变量作为变量传递,因此我不得不使用下面的if
语句。有人知道如何正确地做到这一点吗?我使用的是PHP5.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;
}
测试:
直接作为变量排序时所需的更改传递:
$sort1_type = "SORT_DESC";
$sort2_type = "SORT_ASC";
twoColumnMultiSort($test, 'model', 'year',$sort1_type,$sort2_type);
因此,请删除方法中的if语句。不要在名称周围加引号
$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;
}
不要在名字周围加引号
$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;
}
排序选项不是特殊的关键字,它们只是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)
排序选项不是特殊的关键字,它们只是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)