【问题标题】:Laravel sort array by created_atLaravel 按 created_at 排序数组
【发布时间】:2015-09-18 21:12:02
【问题描述】:

我需要按照 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 ;   

        }

【问题讨论】:

  • 如何查询这个?我的意思是你是怎么得到这种输出雄辩的关系的?怎么样?

标签: arrays sorting laravel laravel-4


【解决方案1】:

我认为您可以使用 laravel 数据库 collection api 而不是 array 来做到这一点,并将项目添加到集合并在集合上使用 sortBy 方法,

$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());

--- ----

试试这个,但我没有测试它。如果这可行,那么这将是最好的方法。

$companyMessage = Company_Message::where('match_id','=',$match_id);
$investorMessage = Investor_Message::where('match_id','=',$match_id);

//$companyMessage & $investorMessage both are laravel database collection,
// so you can use merge method on collection to merge these two collections.

$allResults = $companyMessage->merge($investorMessage);

//use laravel collection sort method to sort the collection by created_at
$allResults = $allResults->sortBy('created_at');

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

【讨论】:

  • 嗨,我都试过了。它没有用。它仍然显示与我发布的内容相似
  • 检查您输出的是排序集合而不是合并集合?
  • 这是我得到的一部分。 "4": { "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" }, "5": { "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" }, "6": { "id": 2, "message": "sup sup", "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" }
  • 当你对它进行 json 编码时请使用toArray()。与更新答案中一样
  • 啊哈是的 :) 好吧,很高兴能帮助你。接受答案和小小的投票将帮助我哈哈。欢呼:)
【解决方案2】:
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 文档中找到

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

【讨论】:

  • 查看两种情况的执行时间
猜你喜欢
  • 2019-11-24
  • 2011-04-05
  • 2021-09-03
  • 2014-01-16
  • 2016-05-10
  • 2013-10-03
  • 2021-11-09
  • 2020-11-21
相关资源
最近更新 更多