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


PHP custom sort on an object array Laravel-5

我需要在对象数组上构建自定义排序,但我不确定从哪里开始。 我已经看到了函数 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
                )
        )
)

你可以试试这样的东西

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