由created_at完成的Laravel排序数组

时间:2015-07-01 15:43:32

标签: arrays sorting laravel laravel-4

我需要通过它的created_at时间戳按时间顺序对我的json数组进行排序。

  [{
    "id": 1,
    "message": "hello",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 12:56:34",
    "updated_at": "2015-07-01 12:56:34"
  }, {
    "id": 2,
    "message": "helloWorld",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 12:56:53",
    "updated_at": "2015-07-01 12:56:53"
  }, {
    "id": 3,
    "message": "sup",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 13:12:53",
    "updated_at": "2015-07-01 13:12:53"
  }, {
    "id": 4,
    "message": "sup",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 13:13:04",
    "updated_at": "2015-07-01 13:13:04"
  }, {
    "id": 5,
    "message": "sup",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 15:06:39",
    "updated_at": "2015-07-01 15:06:39"
  }, {
    "id": 1,
    "message": "yo yo ",
    "investor_id": 35,
    "company_id": 7,
    "match_id": 2,
    "created_at": "2015-07-01 22:09:36",
    "updated_at": "-0001-11-30 00:00:00"
  }, {
    "id": 2,
    "message": "sup nigga",
    "investor_id": 35,
    "company_id": 7,
    "match_id": 2,
    "created_at": "2015-07-01 14:00:00",
    "updated_at": "-0001-11-30 00:00:00"
  }]

任何人都可以教我如何在stackoverflow中尝试过很多解决方案。他们中的许多人说数组不能与“sortBy”一起使用。

这是我的代码的一部分:

$companyMessage = Company_Message::where('match_id','=',$match_id);
    $investorMessage = Investor_Message::where('match_id','=',$match_id);
    $message = array();
    if($companyMessage->count()>0 || $investorMessage->count() > 0){
        if($lastActivityDate == null ){
            //load all messages before 
            if($companyMessage !=null){
                foreach ($companyMessage->get() as $cm) {
                    array_push($message,$cm);
                }
            }

            if($investorMessage !=null){
                foreach($investorMessage->get() as $im){
                    array_push($message,$im);
                }
            }

            return $message ;   

        }

2 个答案:

答案 0 :(得分:1)

我认为您可以使用laravel数据库collection api而不是/tmp/mysql.sock来执行此操作,并将项目添加到集合并在集合中使用sortBy方法,

array

--- ----

尝试这个,但我没有测试过。如果这样做,那么这将是最好的方法。

$col = new \Illuminate\Database\Eloquent\Collection();

if ($companyMessage != null) {
  foreach($companyMessage - > get() as $cm) {
    $col->add($cm);
  }
}
if ($investorMessage != null) {
  foreach($investorMessage - > get() as $im) {
    $col->add($im);    
  }
}

$col = $col->sortBy('created_at');

//return the json
return Response::json($jsonArr->toArray());

答案 1 :(得分:1)

if($companyMessage !=null){
    foreach ($companyMessage->get() as $cm) {
        array_push($message,$cm);                       
    }
}

if($investorMessage !=null){
    foreach($investorMessage->get() as $im){
        array_push($message,$im);
    }
}

$message = array_values(array_sort($message, function ($value) {
    return $value['created_at'];
}));
嘿,伙计,我找到了一个更好的解决方案来排序我的阵列。答案可以在laravel docs

找到

http://laravel.com/docs/5.1/helpers#method-array-sort