Laravel array_sort helper DESC e ASC

I want to sort a multidimensionl array with one or more keys using the Laravel helper array_sort.

array(
    array('firstname_1','lastname_1'),
    array('firstname_2','lastnmae_2')
)

I want to order it first by name and then by name.

I also want to do this in DESC or ASC order. How can I achieve this?

There are functions on the Internet that can be implemented on the Internet, but I would like to understand how to use the Laravel helper. The document for array_sort ( http://laravel.com/docs/helpers#arrays ) I do not think is exhaustive.

+4
source share
2 answers

array_sort() - Illuminate\Support\Collection::sortBy() . , , :

function array_sort($array, Closure $callback)
{
    return Illuminate\Support\Collection::make($array)->sortBy($callback)->all();
}

, . , Collection , .

-, :

  • Laravel PHP array_multisort(). @Jon , - SO.

  • Collection .

# 1.

+4

, Laravel.

:

$array = array(
    array('firstname_1','lastname_1'),
    array('firstname_2','lastname_3'),
    array('firstname_2','lastname_2'),
    array('firstname_3','lastname_3'),
);

, - . Laravel . , :

$array = array_sort($array, function($value) {
    return sprintf('%s,%s', $value[0], $value[1]);
});

Laravel :

$intermediateArray = array(
    'firstname_1,lastname_1',
    'firstname_2,lastname_3',
    'firstname_2,lastname_2',
    'firstname_3,lastname_3',
);

, , .

, :

$array = array_reverse(array_sort($array, function($value) {
    return sprintf('%s,%s', $value[0], $value[1]);
}));
+3

Source: https://habr.com/ru/post/1547425/


All Articles