2015-06-16 37 views
0

我需要在對象數組上創建自定義排序,但我不知道從哪裏開始。我已經看到函數usort()等,但我不明白我將如何實現我需要的這些功能。對象陣列上的PHP自定義排序Laravel-5

我的應用程序正在返回下面的對象數組。我需要按以下順序在「槽」字段中排序:全天,上午,下午。

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 
       ) 

     ) 

) 
+0

你試過的東西也試過了 –

+0

我還沒有嘗試,所以尋找建議如何處理它。 – V4n1ll4

回答

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

+0

非常感謝! – V4n1ll4