public static function multisort(&$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR)
{
$keys = is_array($key) ? $key : [$key];
if (empty($keys) || empty($array)) {
return;
}
$n = count($keys);
if (is_scalar($direction)) {
$direction = array_fill(0, $n, $direction);
} elseif (count($direction) !== $n) {
throw new InvalidParamException('The length of $direction parameter must be the same as that of $keys.');
}
if (is_scalar($sortFlag)) {
$sortFlag = array_fill(0, $n, $sortFlag);
} elseif (count($sortFlag) !== $n) {
throw new InvalidParamException('The length of $sortFlag parameter must be the same as that of $keys.');
}
$args = [];
foreach ($keys as $i => $key) {
$flag = $sortFlag[$i];
$args[] = static::getColumn($array, $key);
$args[] = $direction[$i];
$args[] = $flag;
}
// This fix is used for cases when main sorting specified by columns has equal values
// Without it it will lead to Fatal Error: Nesting level too deep - recursive dependency?
$args[] = range(1, count($array));
$args[] = SORT_ASC;
$args[] = SORT_NUMERIC;
$args[] =& $array;
call_user_func_array('array_multisort', $args);
}