2015-06-16 38 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