对象数组Laravel-5上的PHP自定义排序

xid 发布于 5 天前 arrays 最后更新 5 天前 0 浏览

我需要在对象数组上构建自定义排序,但我不确定从何处开始。我已经看到函数usort()等,但我不明白我将如何实现我需要的这些功能。 我的应用程序正在返回下面的对象数组。我需要按以下顺序在“槽”字段中排序:全天,上午,下午。

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\BookingDates Object
                (
                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 265
                            [slot] => Morning
                        )
[original:protected] => Array
                        (
                            [id] => 265
                            [slot] => Morning
              )
[relations:protected] => Array
                        (
                        )
[hidden:protected] => Array
                        (
                        )
[visible:protected] => Array
                        (
                        )
[appends:protected] => Array
                        (
                        )
[fillable:protected] => Array
                        (
                        )
[guarded:protected] => Array
                        (
                            [0] => *
                        )
[dates:protected] => Array
                        (
                        )
[casts:protected] => Array
                        (
                        )
[touches:protected] => Array
                        (
                        )
[observables:protected] => Array
                        (
                        )
[with:protected] => Array
                        (
                        )
[morphClass:protected] => 
                    [exists] => 1
                )
[1] => App\BookingDates Object
                (
                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 266
                            [slot] => Afternoon
                      )
[original:protected] => Array
                        (
                            [id] => 266
                            [slot] => Afternoon
                      )
[relations:protected] => Array
                        (
                        )
[hidden:protected] => Array
                        (
                        )
[visible:protected] => Array
                        (
                        )
[appends:protected] => Array
                        (
                        )
[fillable:protected] => Array
                        (
                        )
[guarded:protected] => Array
                        (
                            [0] => *
                        )
[dates:protected] => Array
                        (
                        )
[casts:protected] => Array
                        (
                        )
[touches:protected] => Array
                        (
                        )
[observables:protected] => Array
                        (
                        )
[with:protected] => Array
                        (
                        )
[morphClass:protected] => 
                    [exists] => 1
                )
[2] => App\BookingDates Object
                (
                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 267
                            [slot] => All Day
                       )
[original:protected] => Array
                        (
                            [id] => 267
                            [slot] => All Day
                        )
[relations:protected] => Array
                        (
                        )
[hidden:protected] => Array
                        (
                        )
[visible:protected] => Array
                        (
                        )
[appends:protected] => Array
                        (
                        )
[fillable:protected] => Array
                        (
                        )
[guarded:protected] => Array
                        (
                            [0] => *
                        )
[dates:protected] => Array
                        (
                        )
[casts:protected] => Array
                        (
                        )
[touches:protected] => Array
                        (
                        )
[observables:protected] => Array
                        (
                        )
[with:protected] => Array
                        (
                        )
[morphClass:protected] => 
                    [exists] => 1
                )
)
)
我期待的结果是这样的:
Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\BookingDates Object
                (
                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 267
                            [slot] => All Day
                       )
[original:protected] => Array
                        (
                            [id] => 267
                            [slot] => All Day
                        )
[relations:protected] => Array
                        (
                        )
[hidden:protected] => Array
                        (
                        )
[visible:protected] => Array
                        (
                        )
[appends:protected] => Array
                        (
                        )
[fillable:protected] => Array
                        (
                        )
[guarded:protected] => Array
                        (
                            [0] => *
                        )
[dates:protected] => Array
                        (
                        )
[casts:protected] => Array
                        (
                        )
[touches:protected] => Array
                        (
                        )
[observables:protected] => Array
                        (
                        )
[with:protected] => Array
                        (
                        )
[morphClass:protected] => 
                    [exists] => 1
                )
[1] => App\BookingDates Object
                (
                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 265
                            [slot] => Morning
                      )
[original:protected] => Array
                        (
                            [id] => 265
                            [slot] => Morning
                      )
[relations:protected] => Array
                        (
                        )
[hidden:protected] => Array
                        (
                        )
[visible:protected] => Array
                        (
                        )
[appends:protected] => Array
                        (
                        )
[fillable:protected] => Array
                        (
                        )
[guarded:protected] => Array
                        (
                            [0] => *
                        )
[dates:protected] => Array
                        (
                        )
[casts:protected] => Array
                        (
                        )
[touches:protected] => Array
                        (
                        )
[observables:protected] => Array
                        (
                        )
[with:protected] => Array
                        (
                        )
[morphClass:protected] => 
                    [exists] => 1
                )
[2] => App\BookingDates Object
                (
                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 266
                            [slot] => Afternoon
                       )
[original:protected] => Array
                        (
                            [id] => 266
                            [slot] => Afternoon
                        )
[relations:protected] => Array
                        (
                        )
[hidden:protected] => Array
                        (
                        )
[visible:protected] => Array
                        (
                        )
[appends:protected] => Array
                        (
                        )
[fillable:protected] => Array
                        (
                        )
[guarded:protected] => Array
                        (
                            [0] => *
                        )
                [dates:protected] => Array
                    (
                    )

                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

    )

)

已邀请:

baut

赞同来自:

你可以尝试这样的事情

public function index()
{
  $bookingsDate = BookingDate::all() // Your actual collection
  $this->sortCollection($bookingDate);
}
public function sortCollection(\Collection &$bookingDate)
{
   $bookingDate->sortBy(function($date)
   {
     $slots = [
        'Afternoon' => 1,
        'Morning'   => 2,
        'All day'   => 3,
     ];
$slot = $slots[$date['slot']];
return $slot;
   });
}
sortBy方法将执行升序排序,对于降序排序,请尝试sortByDesc 请参阅:http://laravel.com/docs/5.1/collections#method-sortby