Ad

Is There Any Easier Or Shorter Way To Write This Code?

- 1 answer

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?

Ad

Answer

You can use a Laravel collection if you want:

$my_array = collect($my_array)->sortBy('number')->all();
Ad
source: stackoverflow.com
Ad