framework/helpers/BaseArrayHelper.php
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 InvalidArgumentException('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 InvalidArgumentException('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);
    }
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 InvalidArgumentException('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 InvalidArgumentException('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);
    }
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 InvalidArgumentException ( '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 InvalidArgumentException ( '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 ) ; }
Sorts an array of objects or arrays (with the same structure) by one or several keys.
@param array $array the array to be sorted. The array will be modified after calling this method.
@param string|\Closure|array $key the key(s) to be sorted by. This refers to a key name of the sub-array
elements, a property name of the objects, or an anonymous function returning the values for comparison
purpose. The anonymous function signature should be: `function($item)`.
To sort by multiple keys, provide an array of keys here.
@param int|array $direction the sorting direction. It can be either `SORT_ASC` or `SORT_DESC`.
When sorting by multiple keys with different sorting directions, use an array of sorting directions.
@param int|array $sortFlag the PHP sort flag. Valid values include
`SORT_REGULAR`, `SORT_NUMERIC`, `SORT_STRING`, `SORT_LOCALE_STRING`, `SORT_NATURAL` and `SORT_FLAG_CASE`.
Please refer to [PHP manual](https://secure.php.net/manual/en/function.sort.php)
for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.
@throws InvalidArgumentException if the $direction or $sortFlag parameters do not have
correct number of elements as that of $key.
Sorts an array of objects or arrays ( with the same structure ) by one or several keys .