ItzMe42
ItzMe42

Reputation: 211

is there any easier or shorter way to write this code?

Is there a shorter way to write this code?

uasort($my_array, function ($a, $b) {
    if ($a['number'] == $b['number'])
        return 0;
    elseif ($a['number'] < $b['number'])
        return 1;
    else
        return -1;
});

This works, but I need to repeat this code many times.

Is there a better way to do it?

Specifically is there a better way to do it in Laravel?

Upvotes: 0

Views: 83

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219938

You can use a Laravel collection if you want:

$my_array = collect($my_array)->sortBy('number')->all();

Upvotes: 2

Related Questions